0

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

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

0

The Bash syntax is to not put in spaces when assigning values to variables, so do it like this:

p=`/bin/ls`
echo $p

Comments

0

Can't you just write...

/bin/ls

...in one line?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.