Quantcast
Channel: What is the difference between double-quoting and not double-quoting an array in Bash? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

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

$
0
0

While tracking down an error in my shellscript, I found the following behavior in this code snippet:

declare -a filelistreadarray filelist < <(ls -A)readonly filelistfor file in "${filelist[@]}"; do  sha256sum ${filelist[$file]} | head -c 64done

When the array filelist is not in double quotes, the command succeeds. I've been using ShellCheck to try to improve my coding, which recommends-

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[0]} | head -c 64e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855bash-5.0# sha256sum "${filelist[0]}" | head -c 64sha256sum: 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[0]} not contain an additional newline but echo "${filelist[0]}" does?


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images