Skip to content

Instantly share code, notes, and snippets.

@aahnik
Created May 13, 2021 10:04
Show Gist options
  • Save aahnik/d7d21370e643403c60f9a2cf7d52d905 to your computer and use it in GitHub Desktop.
Save aahnik/d7d21370e643403c60f9a2cf7d52d905 to your computer and use it in GitHub Desktop.
Convert a bash string to an array of words. Using the IFS (internal field seperator).
export IFS=" "
str="hello world what is happening"
arr=( $str )
echo first word: ${arr[0]}
echo second word: ${arr[1]}
echo third word: ${arr[2]}
@aahnik
Copy link
Author

aahnik commented May 13, 2021

Output

➜ bash s.sh
first word: hello
second word: world
third word: what

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment