If I do something like this:

echo $x

Then three arguments will be extracted (which are hello and hi and world), and these three arguments will be passed to echo.

But when I do not use a variable:

echo hello,hi,world bye

Then word splitting will happen using the space delimiter and not the comma delimiter, and so the two arguments generated and passed to echo will behello,hi,world and bye.

Is there a way to make word splitting work with a non-space delimiter when not using a variable?

  • Could you give a practical example of where you plan to use this? I assume you don't just want to echo something, and suspect this might be an XY problem. If you give an example of the specific use case you have in mind, we might be able to give you a better solution. – terdon Dec 30 '17 at 11:41
  • @terdon I don't plan to use this anywhere, I am just trying to understand how word splitting works. – user267935 Dec 30 '17 at 12:27
  • Ah, fair enough. In that case, the answer below should be what you need. – terdon Dec 30 '17 at 12:27
  • @user267935, you may also be interested in this answer by Stéphane Chazelas from a while back. – ilkkachu Dec 30 '17 at 12:40

No, word splitting happens only after expansions, not on stuff given directly on the command line (on modern shells, that is). The text in POSIX says:

2.6.5 Field Splitting
After parameter expansion (Parameter Expansion), command substitution (Command Substitution), and arithmetic expansion (Arithmetic Expansion), the shell shall scan the results of expansions and substitutions that did not occur in double-quotes for field splitting and multiple fields can result.

(emphasis mine)

And Bash:

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

I'm not sure that's much of a problem, since you could just replace the commas with spaces if the string is directly in the script. And if it comes from the outside, then splitting usually happens naturally, in a command substitution or when using read, etc.

In the original Bourne shell, the behaviour was a bit different, @Stéphane Chazelas discussed this in an answer to another question a while back

  • Strictly speaking, op didn’t ask about expansions. They asked about a variable. So IFS=,; echo $(echo hello,hi,world bye) should do the trick by invoking an expansion without using a variable per se. – kojiro Dec 30 '17 at 14:20
  • @kojiro, invoking a subshell to avoid the use of a variable is even worse... – ilkkachu Dec 30 '17 at 14:32
  • I wasn’t making a qualitative point about worse or better. I’m just drawing the distinction between an expansion and a name. – kojiro Dec 30 '17 at 14:35

The first splitting of the command line is done on metacharacters:

A character that, when unquoted, separates words. One of the following:
| & ; ( ) < > space tab newline

That's before any expansion, before any word splitting (after expansions).
That happens regardless of the value of IFS.

A coma is not a metacharacter, thus a line (initially) will not be split on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.