The shell has a great feature, where it'll preserve argument quoting across variable expansion when you use "$@", such that the script:
for f in "$@"; do echo "$f"; done
when invoked with arguments:
"with spaces" '$and $(metachars)'
will print, literally:
with spaces
$and $(metachars)
This isn't the normal behaviour of expansion of a quoted string, it seems to be a special case for "$@".
Is there any way to get this behaviour for other variables? In the specific case I'm interested in, I want to safely expand $SSH_ORIGINAL_COMMAND
in a command=
specifier in a restricted public key entry, without having to worry about spaces in arguments, metacharacters, etc.
"$SSH_ORIGINAL_COMMAND"
expands like "$*"
would, i.e. a naïve expansion that doesn't add any quoting around separate arguments.
Is the information required for "$@" style expansion simply not available to the shell in this case, by the time it gets the env var SSH_ORIGINAL_COMMAND
? So I'd instead need to convince sshd to quote the arguments?
The answer to this question is making me wonder if it's possible at all.