If I have a directory named tmp and I enter the following:
ls -l "tmp"
I will get a listing of the file(s) in the tmp directory.
total 4
-rw-rw-rw- 1 brian brian 15 Feb 4 19:34 file1.txt
(I can also use ls -l tmp of course, but my question involves the quoting)
Can anyone here explain the behavior of the following script:
Code:
#!/bin/bash
test1=" ls -l tmp"
echo "1st try" $test1
$test1
test2=" ls -l \"tmp\""
echo "2nd try" $test2
$test2
If I execute this script (which I have named test.sh) via ./test.sh I get the following:
1st try ls -l tmp
total 4
-rw-rw-rw- 1 brian brian 15 Feb 4 19:34 file1.txt
2nd try ls -l "tmp"
ls: "tmp": No such file or directory
So if I TYPE the ls -l "tmp" the command works correctly
And the script echoes the $test1 and $test2 correctly BUT when executing them the quotes are not interpreted correctly.
What is going on here?