Skip to main content
added 83 characters in body
Source Link
ilkkachu
  • 147.8k
  • 16
  • 268
  • 441

That's the same as splitting any variable on spaces. Use word splitting or read:

With word splitting:

var="foo bar"
set -f              # disable globbing
IFS=' '             # make sure IFS contains (just) a space
printf "%s\n" $var  

With read, for a standard shell (if you know there's only two pieces to split into):

var="foo bar"
IFS=' ' read a b <<EOF
$var
EOF
printf "%s\n" "$a" "$b" 

The same with a here-string (Bash/ksh/zsh):

var="foo bar"
IFS=' ' read a b <<< "$var"
printf "%s\n" "$a" "$b" 

With read -a and an arrayin Bash, this works foror read -A in ksh/zsh, you can split the string to an arbitrary number of pieces (Bash)and put them in an array:

var="foo bar"
IFS=' ' read -a arr <<< "$var"     # Bash
printf "%s\n" "${arr[@]}"

YouIn all of the above, you can use $1 in place of $var as usual.

The variants with read also assume the string doesn't contain multiple lines.

However, in Bash you can also split a multi-line string to an array using any whitespace as separator:

IFS=$' \t\n' read -d '' -a arr <<< "$var"

Of course if you have the string in a variable outside the function, and run myFunction $var, the variable will be split to multiple arguments before the function runs.

That's the same as splitting any variable on spaces. Use word splitting or read:

With word splitting:

var="foo bar"
set -f              # disable globbing
IFS=' '             # make sure IFS contains (just) a space
printf "%s\n" $var  

With read, for a standard shell (if you know there's only two pieces to split into):

var="foo bar"
IFS=' ' read a b <<EOF
$var
EOF
printf "%s\n" "$a" "$b" 

The same with a here-string (Bash/ksh/zsh):

var="foo bar"
IFS=' ' read a b <<< "$var"
printf "%s\n" "$a" "$b" 

With read -a and an array, this works for an arbitrary number of pieces (Bash):

var="foo bar"
IFS=' ' read -a arr <<< "$var"
printf "%s\n" "${arr[@]}"

You can use $1 in place of $var as usual.

The variants with read also assume the string doesn't contain multiple lines.

However, in Bash you can also split a multi-line string to an array using any whitespace as separator:

IFS=$' \t\n' read -d '' -a arr <<< "$var"

Of course if you have the string in a variable outside the function, and run myFunction $var, the variable will be split to multiple arguments before the function runs.

That's the same as splitting any variable on spaces. Use word splitting or read:

With word splitting:

var="foo bar"
set -f              # disable globbing
IFS=' '             # make sure IFS contains (just) a space
printf "%s\n" $var  

With read, for a standard shell (if you know there's only two pieces to split into):

var="foo bar"
IFS=' ' read a b <<EOF
$var
EOF
printf "%s\n" "$a" "$b" 

The same with a here-string (Bash/ksh/zsh):

var="foo bar"
IFS=' ' read a b <<< "$var"
printf "%s\n" "$a" "$b" 

With read -a in Bash, or read -A in ksh/zsh, you can split the string to an arbitrary number of pieces and put them in an array:

var="foo bar"
IFS=' ' read -a arr <<< "$var"     # Bash
printf "%s\n" "${arr[@]}"

In all of the above, you can use $1 in place of $var as usual.

The variants with read also assume the string doesn't contain multiple lines.

However, in Bash you can also split a multi-line string to an array using any whitespace as separator:

IFS=$' \t\n' read -d '' -a arr <<< "$var"

Of course if you have the string in a variable outside the function, and run myFunction $var, the variable will be split to multiple arguments before the function runs.

added 268 characters in body
Source Link
ilkkachu
  • 147.8k
  • 16
  • 268
  • 441

That's the same as splitting any variable on spaces. Use word splitting or read:

With word splitting:

var="foo bar"
set -f              # disable globbing
IFS=' '             # make sure IFS contains (just) a space
printf "%s\n" $var  

With read, for a standard shell (if you know there's only two pieces to split into):

var="foo bar"
IFS=' ' read a b <<EOF
$var
EOF
printf "%s\n" "$a" "$b" 

The same with a here-string (Bash/ksh/zsh):

var="foo bar"
IFS=' ' read a b <<< "$var"
printf "%s\n" "$a" "$b" 

With read -a and an array, this works for an arbitrary number of pieces (Bash):

var="foo bar"
IFS=' ' read -a arr <<< "$var"
printf "%s\n" "${arr[@]}"

You can use $1 in place of $var as usual.

The variants with read also assume the string doesn't contain multiple lines.

However, in Bash you can also split a multi-line string to an array using any whitespace as separator:

IFS=$' \t\n' read -d '' -a arr <<< "$var"

Of course if you have the string in a variable outside the function, and run myFunction $var, the variable will be split to multiple arguments before the function runs.

That's the same as splitting any variable on spaces. Use word splitting or read:

With word splitting:

var="foo bar"
set -f              # disable globbing
IFS=' '             # make sure IFS contains (just) a space
printf "%s\n" $var  

With read, for a standard shell (if you know there's only two pieces to split into):

var="foo bar"
IFS=' ' read a b <<EOF
$var
EOF
printf "%s\n" "$a" "$b" 

The same with a here-string (Bash/ksh/zsh):

var="foo bar"
IFS=' ' read a b <<< "$var"
printf "%s\n" "$a" "$b" 

With read -a and an array, this works for an arbitrary number of pieces (Bash):

var="foo bar"
IFS=' ' read -a arr <<< "$var"
printf "%s\n" "${arr[@]}"

You can use $1 in place of $var as usual.

That's the same as splitting any variable on spaces. Use word splitting or read:

With word splitting:

var="foo bar"
set -f              # disable globbing
IFS=' '             # make sure IFS contains (just) a space
printf "%s\n" $var  

With read, for a standard shell (if you know there's only two pieces to split into):

var="foo bar"
IFS=' ' read a b <<EOF
$var
EOF
printf "%s\n" "$a" "$b" 

The same with a here-string (Bash/ksh/zsh):

var="foo bar"
IFS=' ' read a b <<< "$var"
printf "%s\n" "$a" "$b" 

With read -a and an array, this works for an arbitrary number of pieces (Bash):

var="foo bar"
IFS=' ' read -a arr <<< "$var"
printf "%s\n" "${arr[@]}"

You can use $1 in place of $var as usual.

The variants with read also assume the string doesn't contain multiple lines.

However, in Bash you can also split a multi-line string to an array using any whitespace as separator:

IFS=$' \t\n' read -d '' -a arr <<< "$var"

Of course if you have the string in a variable outside the function, and run myFunction $var, the variable will be split to multiple arguments before the function runs.

Source Link
ilkkachu
  • 147.8k
  • 16
  • 268
  • 441

That's the same as splitting any variable on spaces. Use word splitting or read:

With word splitting:

var="foo bar"
set -f              # disable globbing
IFS=' '             # make sure IFS contains (just) a space
printf "%s\n" $var  

With read, for a standard shell (if you know there's only two pieces to split into):

var="foo bar"
IFS=' ' read a b <<EOF
$var
EOF
printf "%s\n" "$a" "$b" 

The same with a here-string (Bash/ksh/zsh):

var="foo bar"
IFS=' ' read a b <<< "$var"
printf "%s\n" "$a" "$b" 

With read -a and an array, this works for an arbitrary number of pieces (Bash):

var="foo bar"
IFS=' ' read -a arr <<< "$var"
printf "%s\n" "${arr[@]}"

You can use $1 in place of $var as usual.