How to send a command with arguments without spaces?
If only there was a variable whose value is a space… Or more generally, contains a space.
cat${IFS}file.txt
The default value of IFS
is space, tab, newline. All of these characters are whitespace. If you need a single space, you can use ${IFS%??}
.
More precisely, the reason this works has to do with how word splitting works. Critically, it's applied after substituting the value of variables. And word splitting treats each character in the value of IFS
as a separator, so by construction, as long as IFS
is set to a non-empty value, ${IFS}
separates words. If IFS
is more than one character long, each character is a word separator. Consecutive separator characters that are whitespace are treated as a single separator, so the result of the expansion of cat${IFS}file.txt
is two words: cat
and file.txt
. Non-whitespace separators are treated separately, with something like IFS=',.'; cat${IFS}file.txt
, cat
would receive two arguments: an empty argument and file.txt
.
I found a way assuming a shell that supports csh
-like brace expansion like ksh
, bash
or yash -o brace-expand
(zsh
supports brace expansion, but not as the first argument like that as that conflicts with command grouping):
{cat,file.txt}
with this way you don't have to use whitespaces in your argument.
One alternative is to use the value of IFS with the expansion of a variable:
$ echo Hello! > file.txt
$ IFS=:
$ a=cat:file.txt
$ $a
Hello!