On my Debian system under bash the following command returns 24 files.
while the following returns none
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
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):
Regards,
Alister
Last edited by alister; 02-27-2011 at 05:19 PM..
Reason: Rewrote explanation for clarity (hopefully)
+ set -x + FIND=-name "*" + find /root/bin -name '"*"' + echo -name '"*"' -name "*"
+ touch '/root/bin/"foo"'
+ 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
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)