Prevent Bash word splitting in substring

How can I prevent bash from splitting words inside a substring? Here is a somewhat contrived example illustrating the problem:

touch file1 'foo bar' FILES="file1 'foo bar'" ls -la $FILES 

Is it possible to get "foo bar" as one line with the ls command in $ FILES, which would lead to the same behavior as the following command?

 ls -la file1 'foo bar' 
+7
source share
2 answers

Use an array:

 files=( file1 'foo bar' ) ls -la "${files[@]}" 
+14
source

The kojiro array solution is the best option here. To introduce another option, you can save the list of files in FILES using a different field separator than a space, and set IFS to this field separator

 OLDIFS=$IFS FILES="file1:foo bar" IFS=':'; ls -la $FILES IFS=$OLDIFS 
+2
source

All Articles