Answers for "split a string in bash into variables"

1

How to split a string in bash

string="you got a friend in me"
IFS=' ' read -ra split <<< "$string"
echo "${split[*]}"
# Output: you got a friend in me
echo "${split[3]}"
# Output: friend
Posted by: Guest on March-14-2022
1

bash split string into variables

# separator is space
VAR="inforge srl bergamo"
read -r ONE TWO THREE <<< "${VAR}"
echo ${ONE}
echo ${TWO}
echo ${THREE}

# separator is comma
VAR="inforge,srl,bergamo"
IFS="," read -r ONE TWO THREE <<< "${VAR}"
echo "${ONE} ${TWO} ${THREE}"
Posted by: Guest on September-24-2020

Code answers related to "split a string in bash into variables"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language