17

Is there a way to execute a command with arguments in linux without whitespaces?

cat file.txt

needs to be:

cat(somereplacementforthiswhitespace)file.txt
2
  • if the file name is " file.txt" then can you can use cat \ file.txt (try \ followed by tab completion)
    – rob
    Mar 14, 2017 at 9:22
  • This 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)
    – Rob
    Mar 14, 2017 at 9:29

4 Answers 4

29

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.

8
  • This is great! How does this work? Why does ${IFS%?} evaluate to a space?
    – loneboat
    May 2, 2019 at 19:17
  • Awesome, that makes sense. I found documentation on the IFS var, but didn't understand the rest. Thank you for explaining!
    – loneboat
    May 2, 2019 at 20:16
  • 1
    @loneboat Actually, while my comment was correct, it was a bit misleading. I've edited my answer with a more detailed explanation. May 2, 2019 at 20:25
  • Saved the day after a long search Nov 26, 2020 at 9:57
  • /bin/bash: sleep${IFS}0: command not found Apr 2, 2022 at 20:51
9

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.

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!
-2

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!

5
  • would this downvoter mind to explain his downvote? Oct 20, 2023 at 16:54
  • I suppose you're using 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. Dec 6, 2023 at 8:55
  • cat<* works in bash too, but ya, you're correct, I am using zsh, thanks for the information! Dec 6, 2023 at 9:36
  • What does your ending comment mean: please don't use it in any unethical way thank you! Dec 6, 2023 at 9:42
  • 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). Dec 6, 2023 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .