quotes in shell variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting quotes in shell variables
# 1  
Old 02-27-2011
quotes in shell variables

On my Debian system under bash the following command returns 24 files.
Code:
 find /root/bin/ -name '*'

while the following returns none
Code:
 FIND="-name '*'"; find /root/bin/ $FIND

Asking on the Debian Users list I've been shown work arounds but not explanations. I'm hoping someone here can explain 'why' $FIND is not being interpreted by find as I expected. Thanks, Mike McClain
# 2  
Old 02-27-2011
If you enable tracing with "set -x", bash will print each command after it's done all the expansions and substitutions. You'll probably be able to see where things diverge.

As the shell's parser consumes the command line, it treats all text within unescaped single quotes literally. In this case, the asterisk in your first command is never expanded and remains an asterisk. When the shell is done parsing the command line, it removes those quotes (quote removal is the final step in sh parsing) and passes the literal asterisk as one of the arguments to the find command.

In the second version, using $FIND, the quotes contained in $FIND's value are NOT part of the command line, so they are not special. They do not appear until after the shell has done parameter expansion on $FIND. So, since those single quotes are not part of the initial command line, they do not protect the asterisk from being treated as a pathname expansion wildcard. The penultimate step during sh parsing, just before quote removal, is this pathname expansion step. The shell, having already done paremeter expansion at this point, is seeing '*', those single quotes are not special, and it will try to match that pattern against pathnames. Unless you have files/directories in the current directory whose names begin and end with a single quote, there will be no match. When a pathname expansion pattern does not match anything, the shell leaves it alone. So, when the find command runs, it will run with an argument of single quote, asterisk, single quote (assuming no match).

You may be wondering, "well, if the single quotes aren't special, why is the asterisk?". Quoted strings are identified before parameter expansion occurs; the pathname expansion step happens afterwards.

It'll probably work as you intend if you use eval, although there are important caveats if that route is chosen (mostly security related):
Code:
eval find /root/bin/ $FIND

Regards,
Alister

Last edited by alister; 02-27-2011 at 05:19 PM.. Reason: Rewrote explanation for clarity (hopefully)
# 3  
Old 02-27-2011
Code:
set -x; FIND='-name "*"';find /root/bin $FIND; echo $FIND

+ set -x + FIND=-name "*" + find /root/bin -name '"*"' + echo -name '"*"' -name "*"
Code:
 touch '/root/bin/"foo"'

+ touch '/root/bin/"foo"'
Code:
set -x; FIND='-name "*"';find /root/bin $FIND; echo $FIND

+ set -x + FIND=-name "*" + find /root/bin -name '"*"' /root/bin/"foo" + echo -name '"*"' -name "*" You'll note the file with quotes in the name. Looks to me that bash puts an extra set of quotes around the quoted term in the variable upon expansion. I can't think why that would be. I don't think this is going to display properly, sorry. Thanks for your help, Mike
# 4  
Old 02-27-2011
The trace output is correct. It adds those quotes because that's the proper equivalent of the command it's executing (after all expansions and substitutions) had it been typed at the terminal.

Last edited by alister; 02-27-2011 at 08:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

2. Shell Programming and Scripting

Bash shell adding extra single quotes

AIX 6.1 bash shell #!/bin/bash -x STATEMENT="cvs commit -m \"This is\" ../PBP/EIR.ENTRY" echo $STATEMENT exit 0 This is the output + STATEMENT='cvs commit -m "This is" ../PBP/EIR.ENTRY' + echo cvs commit -m '"This' 'is"' ../PBP/EIR.ENTRY cvs commit -m "This is" ../PBP/EIR.ENTRY + exit... (26 Replies)
Discussion started by: hpodhrad
26 Replies

3. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

4. Shell Programming and Scripting

shell script - to append single quotes and comma

file1 ---- 34556745 32678343 31576776 31455566 21356666 I want to assign the record values to a variable in the below format, so that I can use output in .sql file for querying in database. ('34556745', '32678343', '31576776', '31455566', '21356666') ----------- below is the... (11 Replies)
Discussion started by: rajivrsk
11 Replies

5. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

6. Shell Programming and Scripting

Use variables with double quotes sed -i

I have the following line of code: sed -i "/MatchText/ s/${tgrepLine}/${tNewLine}/" filename.outputfilename.output contains this: blablabla PATH=".:/home/root/bin/:/usr/local/bin/" blablablaVariable ${tgrepLine} contains: PATH=".:/home/root/bin/:/usr/local/bin/" Variable ${tNewLine}... (3 Replies)
Discussion started by: inspire87
3 Replies

7. Shell Programming and Scripting

How can i use single quotes for SQL command in shell script

Hi. please help me to write the following query in a shell script. the Query is :select no,salary from emp_info where name='$var_name' the following is my code. #! /bin/sh var_name=$1 sqlplus -s user/pwd@DB << EOF select no,salary from emp_info where name="'$var_name'";... (4 Replies)
Discussion started by: little_wonder
4 Replies

8. Shell Programming and Scripting

how to use in bash variables and quotes

I have some troubles with variables and quotes... I want: if $URL is empty (no user input) go to http://www.localhost/index.php/ else add this string (search) "?s=+$URL" EXAMPLE: No user input string= http://www.localhost/index.php/ User input = "unix" string=... (3 Replies)
Discussion started by: aspire
3 Replies

9. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

10. AIX

how to pass variables surrounded in double quotes to awk?

Hi, I'm making progress on this but hung up on one last detail. I'd like to use AWK to pass the system date and time(among other things) to the first line of a file. Here's what I have: BEGIN {TOTALPP = 0;FREEPP=0;USEDPP=0;print "LPAR NAME:",lpar,"DATE:",tdate } I call AWK with the... (4 Replies)
Discussion started by: cruiser
4 Replies
Login or Register to Ask a Question