Sample Header Ad - 728x90

What is the difference between double-quoting and not double-quoting an array in Bash?

4 votes
3 answers
2100 views
While tracking down an error in my shellscript, I found the following behavior in this code snippet: declare -a filelist readarray filelist Double quote to prevent globbing and word splitting. I'm not worried about word splitting in this case, but in a lot of other cases I am, so I'm trying to keep my code consistent. However, when I double quote the array, the command fails. Simplifying the code to a single element gives the following: bash-5.0# sha256sum ${filelist} | head -c 64 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 bash-5.0# sha256sum "${filelist}" | head -c 64 sha256sum: can't open 'file1 ': No such file or directory I can obviously just... not double quote because in this instance word splitting isn't a concern. But I wanted to post because in the future it might be. My question has two parts: 1. Is there a "best-practices" way to prevent word splitting other than double quoting the array as above? 2. Where are the single quotes coming from in the array? Edit: there are no single quotes. The single quotes are the error showing the name of the file that cannot be opened. Also, just out of curiosity, why does echo ${filelist} not contain an additional newline but echo "${filelist}" does?
Asked by dcwaters (461 rep)
Apr 23, 2020, 03:53 PM
Last activity: Apr 24, 2020, 07:07 AM