Author |
Message |
Lasse Klieman #1 / 12
|
 Single quotes in variable
What is wrong with the following code? [work 2] cat t #!/bin/sh -x com='sh -c '\''echo $1'\' $com x one two Execution gives: [work 2] ./t + com=sh -c 'echo $1' + sh -c 'echo $1' x one two Syntax error: Unterminated quoted string However, when I copy&paste the command, it works: [work 2] sh -c 'echo $1' x one two one Thanks! Lasse
|
Fri, 26 Sep 2008 01:17:29 GMT |
|
 |
Xicheng Ji #2 / 12
|
 Single quotes in variable
what shell are you using? I guess you mixed up shell variables and aliases/functions under bash, you can use a function: com() { sh -c "echo $2" } under csh/tcsh, use an alias: alias com 'sh -c "echo \!:2"' then run: com x one two you get: one Xicheng
|
Fri, 26 Sep 2008 01:50:56 GMT |
|
 |
Xicheng Ji #3 / 12
|
 Single quotes in variable
== com() { sh -c "echo $2" } sorry I missed a simicolon here: com() { sh -c "echo $2"; } Xicheng
|
Fri, 26 Sep 2008 01:54:50 GMT |
|
 |
Lasse Klieman #4 / 12
|
 Single quotes in variable
pdksh (under NetBSD).
I know that :-). I need it in the form of a variable, because I'd like to use it with xargs. I'd like to do: find ... | xargs $com xargs needs an executable, not a shell function. Hence, I encapsulate my shell function into the sh -c '...' construct. Because I use it often in a script, I'd like to store it in a variable. (An alternative is to realize the shell function in the form of an extra script.) Thanks for your answer, Lasse
|
Fri, 26 Sep 2008 01:59:29 GMT |
|
 |
Xicheng Ji #5 / 12
|
 Single quotes in variable
oo, I see, but I guess you need to use an "eval" to make it executable: eval "$com x one two" Xicheng on your variable
|
Fri, 26 Sep 2008 02:14:44 GMT |
|
 |
Bill Marcu #6 / 12
|
 Single quotes in variable
On Sun, 9 Apr 2006 19:17:29 +0200, Lasse Kliemann
Try this: eval $com x one two -- Most legends have their basis in facts. -- Kirk, "And The Children Shall Lead", stardate 5029.5
|
Fri, 26 Sep 2008 03:46:05 GMT |
|
 |
Chris F.A. Johnso #7 / 12
|
 Single quotes in variable
On 2006-04-09, Lasse Kliemann wrote:
From the bash man page (it applies to all Bourne-type shells): Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
-- Chris F.A. Johnson, author | <http://cfaj.freeshell.org> Shell Scripting Recipes: | My code in this post, if any, A Problem-Solution Approach | is released under the 2005, Apress | GNU General Public Licence
|
Fri, 26 Sep 2008 05:27:59 GMT |
|
 |
Icarus Sparr #8 / 12
|
 Single quotes in variable
Interesting quote, but not applicable here. Look how the single quotes pair up. com='sh -c '\''echo $1'\' \____/ \______/ The value of com should be sh -c 'echo $1' So there are no single quotes between single quotes.
|
Fri, 26 Sep 2008 11:27:00 GMT |
|
 |
M. ?hma #9 / 12
|
 Single quotes in variable
I use this frequently in my scripts: find /dir -print0 | xargs -0 -n 1 sh -c ' # do something with "$1" # remember not to put single quotes in here ' "$0" The last argument is put in $0 when the "inside" script is run. "$0" give it the name of the "outside" script. Without it the file name from xargs will end up in $0. This is from the bash man page; ksh work the same way: -c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0. You can replace '<script>' with a variable, and the variable can contain single quotes. Magnus On Sun, 9 Apr 2006 19:59:29 +0200
|
Fri, 26 Sep 2008 21:10:24 GMT |
|
 |
Lasse Klieman #10 / 12
|
 Single quotes in variable
Yes, this works. But I do not understand why the other thing did not. Output from my script is now: [work 2] ./t + com=sh -c 'echo $1' + eval sh -c 'echo $1' x one two + sh -c echo $1 x one two one Thanks for all your answers!
|
Sat, 27 Sep 2008 01:14:28 GMT |
|
 |
M. ?hma #11 / 12
|
 Single quotes in variable
Got it! Quoting recognition happens before parameter expansion, not after. Therefore the -c code sh get is "'echo", with "$1'" as the first argument (the script name). Magnus On Mon, 10 Apr 2006 19:14:28 +0200
|
Sat, 27 Sep 2008 03:36:04 GMT |
|
 |
Lasse Klieman #12 / 12
|
 Single quotes in variable
Just to make sure I understand: It would be the responsibility of quoting recognition to realize that the single quotes surround the inner script (i.e., echo $1). However, at the time when quoting recognition takes place, the contents of the variable com is not yet available, because parameter expansion has not taken place before.
|
Sat, 27 Sep 2008 03:39:16 GMT |
|
 |
|