Note that there are some explanatory texts on larger screens.

plurals
  1. POBash eval replacement $() not always equivalent?
    primarykey
    data
    text
    <p>Everybody says eval is evil, and you should use $() as a replacement. But I've run into a situation where the <em>unquoting</em> isn't handled the same inside $().</p> <p>Background is that I've been burned too often by file paths with spaces in them, and so like to quote all such paths. More paranoia about wanting to know where all my executables are coming from. Even more paranoid, not trusting myself, and so like being able to display the created commands I'm about to run.</p> <p>Below I try variations on using eval vs. $(), and whether the command name is quoted (cuz it <em>could</em> contain spaces)</p> <pre><code> BIN_LS="/bin/ls" thefile="arf" thecmd="\"${BIN_LS}\" -ld -- \"${thefile}\"" echo -e "\n Running command '${thecmd}'" $($thecmd) Running command '"/bin/ls" -ld -- "arf"' ./foo.sh: line 8: "/bin/ls": No such file or directory echo -e "\n Eval'ing command '${thecmd}'" eval $thecmd Eval'ing command '"/bin/ls" -ld -- "arf"' /bin/ls: cannot access arf: No such file or directory thecmd="${BIN_LS} -ld -- \"${thefile}\"" echo -e "\n Running command '${thecmd}'" $($thecmd) Running command '/bin/ls -ld -- "arf"' /bin/ls: cannot access "arf": No such file or directory echo -e "\n Eval'ing command '${thecmd}'" eval $thecmd Eval'ing command '/bin/ls -ld -- "arf"' /bin/ls: cannot access arf: No such file or directory $("/bin/ls" -ld -- "${thefile}") /bin/ls: cannot access arf: No such file or directory </code></pre> <p>So... this is confusing. A quoted command path is valid everywhere except inside a $() construct? A shorter, more direct example:</p> <pre><code>$ c="\"/bin/ls\" arf" $ $($c) -bash: "/bin/ls": No such file or directory $ eval $c /bin/ls: cannot access arf: No such file or directory $ $("/bin/ls" arf) /bin/ls: cannot access arf: No such file or directory $ "/bin/ls" arf /bin/ls: cannot access arf: No such file or directory </code></pre> <p>How does one explain the simple <code>$($c)</code> case?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload