Shell Builtin ‘eval’
susam.net
susam.net
$ ls -la "foo bar"
-rw------- 1 u0_a298 u0_a298 6 Jan 10 00:22 'foo bar'
$ cmd=(cat "foo bar")
$ "${cmd[@]}"
hello
Edit, since someone pointed out you the article is using dash, something I overlooked, you can still avoid eval by using set to overload the script args: $ set -- cat "foo bar"
$ "$@"
hello
Not sure how this would work if you still need the values of the current args. I don't think there is a way set a variable to be the current "$@" to be used later. However, if you don't need the current args any more, this is a possible solution to avoid eval.During the same task, I also discovered that a for loop on it's own is run inside the same shell as the code around it (duh), but a for loop which is feeding a pipe is run in a subshell. This was a surprise, because I wanted to set some variables in the for loop, piping progress indications to the `dialog` program, and then use those variables afterwards. My variables kept disappearing after the for loop exited, but not when I removed the progress indications.
Shell sucks.
i=0
for foo in …; do
i=((i + 1))
done > >(stuff | goes | here)To stay compatible with POSIX one needs to use either named pipes or extra file discriptors to communicate from the sub shell.
And with containers bash may not be even there.
$ echo lorem ipsum > "foo bar"
$ cmd='cat "foo bar"'
$ trap "$cmd" ERR;test;trap - ERR
lorem ipsum