How to split a string in Bash
Splitting a string in bash
is easy with the help of the internal field separator (IFS
).
Bash string split using IFS
1string="you got a friend in me"
2IFS=' ' read -ra split <<< "$string"
3echo "${split[*]}"
4# Output: you got a friend in me
5echo "${split[3]}"
6# Output: friend
The above method does not interfere with the IFS
global variable since it’s only set for that single invocation.