It is currently Wed, 22 Mar 2023 05:14:55 GMT

Author Message
 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


 Fri, 26 Sep 2008 01:17:29 GMT   
 Single quotes in variable

what shell are you using? I guess you mixed up shell variables and

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


- Show quoted text -

 Fri, 26 Sep 2008 01:50:56 GMT   
 Single quotes in variable
==         com() { sh -c "echo $2" }
sorry I missed a simicolon here:
        com() { sh -c "echo $2"; }


- Show quoted text -

 Fri, 26 Sep 2008 01:54:50 GMT   
 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,

 Fri, 26 Sep 2008 01:59:29 GMT   
 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"


on your variable

- Show quoted text -

 Fri, 26 Sep 2008 02:14:44 GMT   
 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   
 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

   Chris F.A. Johnson, author   |    <>
   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   
 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   
 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.


On Sun, 9 Apr 2006 19:59:29 +0200

 Fri, 26 Sep 2008 21:10:24 GMT   
 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

Thanks for all your answers!

 Sat, 27 Sep 2008 01:14:28 GMT   
 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).


On Mon, 10 Apr 2006 19:14:28 +0200

 Sat, 27 Sep 2008 03:36:04 GMT   
 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   
   [ 12 post ] 

Similar Threads

1. replace single quote by escaped single quote

2. variable interpolation within single quotes

3. How to interpret variable but put inside single quotes (in isql -Q option)

4. ? about single quote-double quote

5. replace single quote to double quote with an example

6. replace single quote to double quote with an example

7. Problem in sed using double quotes and single quotes

8. use of quotes in assignment of variable to variable

9. set command ignoring single/double quotes

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software