binpash / try

Inspect a command's effects before modifying your live system
MIT License
5.17k stars 64 forks source link

shell quoting broken #155

Closed ezrizhu closed 2 months ago

ezrizhu commented 3 months ago

try bash -c "echo a" actually executes bash -c echo a

mgree commented 2 months ago

The solution is to appropriately quote when we generate $script_to_execute. A basic version of this would not be too hard, but subtleties of escaping abound.

Would be easily resolved in a rewrite not in the shell.

mgree commented 2 months ago
for arg in "$@"
do
    case $arg in
        (*[$(printf " \n\t()<>&;|\`?*\$\"'")\\]*)
          quoted=$(echo "$arg" | sed -e "s/'/'\\\\''/")
          printf "'%s' " "$quoted";;
        (*)   printf "%s " "$arg";;
    esac
done
printf "\n"

is a workable first cut.

mgree commented 2 months ago

Our tests expect that try "echo hi>foo" will preserve the redirect and that try "echo hi; echo bye" will preserve the pair of commands.

I don't see how we can support both this existing behavior and appropriate quoting of things like bash -c. An advantage of the current behavior is that you can run try "bash -c \"echo hi\"" and get the right behavior... whereas it's not clear how to preserve the shell syntax with this modified behavior.

Unless you have a better idea, I'll delete the branch and close the issue.

ezrizhu commented 2 months ago

I don't have a better idea, feel free to delete the branch. I'll close the issue once the docs has been updated.