How to send a command with arguments without spaces?
Solution 1
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
.
Solution 2
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.
Solution 3
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!
Related videos on Youtube
Rob
Updated on September 18, 2022Comments
-
Rob over 1 year
Is there a way to execute a command with arguments in linux without whitespaces?
cat file.txt
needs to be:
cat(somereplacementforthiswhitespace)file.txt
-
rob about 7 yearsif the file name is " file.txt" then can you can use cat \ file.txt (try \ followed by tab completion)
-
Rob about 7 yearsThis is an example, i want to use more commands then only cat :( btw, I can't use any sort of whitespace, (tab is a whitespace)
-
-
loneboat about 5 yearsThis is great! How does this work? Why does ${IFS%?} evaluate to a space?
-
loneboat about 5 yearsAwesome, that makes sense. I found documentation on the IFS var, but didn't understand the rest. Thank you for explaining!
-
Gilles 'SO- stop being evil' about 5 years@loneboat Actually, while my comment was correct, it was a bit misleading. I've edited my answer with a more detailed explanation.
-
Babak Bandpey over 3 yearsSaved the day after a long search
-
Peter Kionga-Kamau about 2 years
/bin/bash: sleep${IFS}0: command not found
-
Gilles 'SO- stop being evil' about 2 years@PeterKionga-Kamau The context of the question was something like
bash -c 'sleep${IFS}0'
. Not something likevar='sleep${IFS}0'; bash -c "$var"
-
Peter Kionga-Kamau about 2 years@Gilles'SO-stopbeingevil' noted & thanks, but would love to know how to do it from
$var
which is the only option in my case... hoping it's possible with the correct escaping of characters perhaps... -
Gilles 'SO- stop being evil' about 2 years@PeterKionga-Kamau I don't know what you're trying to do here. Please post a new question with all the information.