In a rather large project, I run into the following problem: while calculating the correct command in a bash script, I don't manage to make the script respect the double quote necessary for single arguments with a blank.
$ ./testo "two three"
ls -l "two three_DD_FF" # <- Why does it look for two files HERE??
ls: cannot access '"two': No such file or directory
ls: cannot access 'three_DD_FF"': No such file or directory
"two three" is the single argument.
With two_three my code works, as to be expected:
$ ./testo two_three
ls -l "two_three_DD_FF"
ls: cannot access '"two_three_DD_FF"': No such file or directory
And this is the basic generic code that I could come up with:
#!/usr/bin/bash
cmd="ls -l"
trio=""'"'"${1}_DD"
trio="${cmd} ${trio}_FF"'"'""
echo ${trio}
${trio}
(I didn't find a less strange method than the one above for the strangely looking strong quote, but that is not my main concern here.)
What I don't understand is the problem in execution:
ls -l "two three_DD_FF"
in the line above still leads to searching for two files, despite of the double quotes; while the same line on the command prompt does a job as intended (that is, searching for file "two three_DD_FF":
$ ls -l "two three_DD_FF"
-rw-rw-r-- 1 myhome me 0 Aug 27 13:09 'two three_DD_FF'
That is, the line echo ${trio} shows a correct command (when executed on the command line, at least; while the same command, when executed from within a script, fails. It fails to respect the double quotes for the file name to be searched.
My questions:
- Why does the same command fail from within a script?
- Of course, how do I rectify the script, so that the call actually results in the correct output?
lscould be some weird alias check like thistype ls${trio}it is always going to split on spaces no matter the content. You have to write constructs in the script that prevent splitting, but using quotes that you write, not generate, for examplels -l "$trio". You can't store commands in a variable - use function to store commands. Required reading mywiki.wooledge.org/BashFAQ/050 .