Bash – Spread bash argument by whitespace

bashshell-script

In my-script, "$1" is a string of tokens, separated by whitespace.

how can I "spread" this single argument into multiple arguments to pass to another program for example

./my-script2 ...$1  # spread the string into multiple arguments, delineating by whitepace

Hopefully you understand the question, had trouble searching for answer to this.

I tried this:

./my-script2 <<< "$1"

and this:

./my-script2 "" <<< $1

and other combinations, but that didn't seem to work. I need to support Bash v3 and above.

Best Answer

./my-script2 $1

Unquoted parameters are subject to word splitting when expanded:

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

This is the specified POSIX behaviour, though some shells (notably zsh) disable it by default. If you've changed the value of IFS from the default, that's on you.

Related Question