0

Look at this bash script (test.sh):

echo "START"
echo $1
echo "END"

Here is how I am launching the script:

./test.sh `ls`

What I want to understand is when ls command is executed.

Do you think ls is executed before test.sh call, or do you think ls is executed inside the test.sh script on line 2.

What I want to do is to be sure the ls call is fired inside test.sh. How can I do this ?

2
  • Prefer "$(command)" over back ticks. However nether should not be used here. Oct 30 '20 at 10:18
  • Warning: command injection is prone to security vulnerabilities. However using this technique inside a program can be useful. Oct 30 '20 at 10:19
4

ls is run before test.sh. It is run as a result of command substitution, which is one of the word expansions which the shell performs in order to construct the final set of arguments which are provided to the command (test.sh in this case); they have to all be finished before the command can run.

If you want to give test.sh a command that it should run itself, skip the command substitution. Change test.sh to

echo "START"
"$1"
echo "END"

and call it using ./test.sh ls.

4
  • It does not work. ls is runned before script...
    – Bob5421
    Oct 30 '20 at 11:16
  • How do you see that? Oct 30 '20 at 11:22
  • Instead of calling ls i've called a script which logs
    – Bob5421
    Oct 30 '20 at 11:37
  • 1
    OK, but how does that tell you that your script isn’t called from test.sh? Oct 30 '20 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.