1

shellcheck gives me the following warning:

In myscript line 38:
        echo -e "blah/blah\n$(cat ${tmpdir}/${filename}.jpdf)" > "$tmpdir"/"$filename".jpdf
                                            ^-- SC2086: Double quote to prevent globbing and word splitting.

The command in question is intended to insert a line at the beginning of a file ${tmpdir}/${filename}.jpdf.

Does the warning by shellcheck make sense? Why?

I have already double quote the entire argument to echo -e, and should I further double quote ${tmpdir} and ${filename}?

Thanks.

Tim
  • 98,580
  • 191
  • 570
  • 977
  • 2
    Related: [How are double quotation marks in `bash` matched (paired)?](https://unix.stackexchange.com/questions/421729/how-are-double-quotation-marks-in-bash-matched-paired) – Weijun Zhou Feb 04 '18 at 03:14
  • 1
    Note that `$(...)` introduces a subshell and in that subshell variables still need to be quoted. – glenn jackman Feb 04 '18 at 12:14

1 Answers1

4

Yes if you want to avoid splitting&globbing like this:

echo -e "blah/blah\n$(cat "${tmpdir}/${filename}.jpdf")" > "$tmpdir/$filename.jpdf"

Just internal double quotes. I mean, inside the $(…) which is already double quoted on the outside "$(…)"

  • The syntax highlight of UL SE seems buggy in this case. – Weijun Zhou Feb 04 '18 at 03:14
  • Then how can `$(cat ${tmpdir}/${filename}.jpdf)` be expanded in your change? – Tim Feb 04 '18 at 03:40
  • @Tim Have you tried it? The quoting start a fresh inside `$(…)`. I seem to be failing to understand your comment, care to elaborate? –  Feb 04 '18 at 03:46
  • In `"blah/blah\n$(cat "${tmpdir}/${filename}.jpdf")"`, why is the second double quote not the end of the first double quote (as shown by the syntax highlighting in your reply), but a start of a string inside `$(...)`? – Tim Feb 04 '18 at 04:12
  • @Tim [Please read and **try**](https://unix.stackexchange.com/a/421747/265604), in short, the quoting starts again inside a `$(…)`. –  Feb 04 '18 at 04:29