Is there a way to execute a command with arguments in linux without whitespaces?
cat file.txt
needs to be:
cat(somereplacementforthiswhitespace)file.txt
Is there a way to execute a command with arguments in linux without whitespaces?
cat file.txt
needs to be:
cat(somereplacementforthiswhitespace)file.txt
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!
OP was 6 years ago,
time goes fast.
But I'm posting this for anyone that any of the previous answers didn't work for him,
like me personally for cat${IFS%???}file.txt
or {cat,*}
:
command not found: cat file.txt
and no matches found: ,cat,*
Anyhow, i found a much simpler approach that worked for me which is:
cat<index.js
or for example:
cat<*
please don't use it in any unethical way thank you!
zsh
(the only shell where cat<*
actually works, and where cat${IFS%???}file.txt
would return that error). Note that that method can't be extended to commands that take arguments. You could do echo${=IFS}test${=IFS}*
though where ${=IFS}
is used so $IFS
be subject to split+glob like it is in other Bourne-like shells.
Commented
Dec 6, 2023 at 8:55
cat<*
works in bash too, but ya, you're correct, I am using zsh, thanks for the information!
Commented
Dec 6, 2023 at 9:36
cat<*
only works in bash if *
happens to expand to one and only one file (and bash is running interactively or with the posix option disabled).
Commented
Dec 6, 2023 at 9:53