Skip to main content
added 12 characters in body
Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

If your file will only ever be a single line you can use:

IFS=$' \t' read -ara array < file.txt 

If it can have multiple lines one way to do it would be to first transform the file so that every word has it's own line and then use bash's readarray (only available in bash 4.0 or later which might be an issue for you if you are on macos):

readarray -t array < <(tr -s '[:space:]' '\n' < file.txt) 

If your file will only ever be a single line you can use:

read -a array < file.txt 

If it can have multiple lines one way to do it would be to first transform the file so that every word has it's own line and then use bash's readarray (only available in bash 4.0 or later which might be an issue for you if you are on macos):

readarray -t array < <(tr -s '[:space:]' '\n' < file.txt) 

If your file will only ever be a single line you can use:

IFS=$' \t' read -ra array < file.txt 

If it can have multiple lines one way to do it would be to first transform the file so that every word has it's own line and then use bash's readarray (only available in bash 4.0 or later which might be an issue for you if you are on macos):

readarray -t array < <(tr -s '[:space:]' '\n' < file.txt) 
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

If your file will only ever be a single line you can use:

read -a array < file.txt 

If it can have multiple lines one way to do it would be to first transform the file so that every word has it's own line and then use bash's readarray (only available in bash 4.0 or later which might be an issue for you if you are on macos):

readarray -t array < <(tr -s '[:space:]' '\n' < file.txt)