i am new to shell scripting. My objective is 开发者_开发知识库to execute the ls command through a shell script but it does not return anything. Mine is a BASH shell
set p = `/bin/ls`
echo $p
Where am i going wrong
For bash the way to do it is
p="$(/bin/ls)"
# Could have done p=`/bin/ls` too, but $( is the newer way
echo "$p"
The set
command is not for standard variable assignment.
A simple
p=`/bin/ls`
will suffice although I prefer the $()
construct since it's easily nestable:
pax$ p=$(/bin/ls)
pax$ echo $p
clients.dat clientupdate.sh
What your set
does (after processing all the valid switches to set shell attributes, of which there are none) is to assign the parameters to the $n
arguments:
pax$ set p = `/bin/ls`
pax $ echo xx $1 xx $2 xx $3 xx $4 xx $5
xx p xx = xx clients.dat xx clientupdate.sh xx xx
From the man bash_builtins
page:
Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to
$1, $2, ... $n
.
The Bash syntax is to not put in spaces when assigning values to variables, so do it like this:
p=`/bin/ls`
echo $p
Can't you just write...
/bin/ls
...in one line?
精彩评论