How can I prevent Bash from splitting words within a substring? Here's a somewhat contrived example to illustrate the problem:
touch file1 'foo bar'
FILES="file1 'foo bar'"
ls -la $FILES
Is it possible to get 'foo bar' regarded as a single string by the ls command within $FILES that would effectively result in the same behavior as the following command?
ls -la file1 'foo bar'
Use an array:
files=( file1 'foo bar' )
ls -la "${files[@]}"
kojiro's array solution is the best option here.
Just to present another option you could store your list of files in FILES
with a different field separator than whitespace and set IFS
to that field separator
OLDIFS=$IFS
FILES="file1:foo bar"
IFS=':'; ls -la $FILES
IFS=$OLDIFS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With