From 41d86317abdf8704fb60a2bfc57f04d49509d03d Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 24 Apr 2021 16:05:10 -0700 Subject: [PATCH] Updated SC2048 (markdown) --- SC2048.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/SC2048.md b/SC2048.md index bdf3d51..968ccda 100644 --- a/SC2048.md +++ b/SC2048.md @@ -1,26 +1,30 @@ ## Use "$@" (with quotes) to prevent whitespace problems. +Or: Use "${array[@]}" (with quotes) to prevent whitespace problems. + ### Problematic code: ```sh cp $* ~/dir +cp ${array[*]} ~/dir ``` ### Correct code: ```sh cp "$@" ~/dir +cp "${array[@]}" ~/dir ``` ### Rationale: -`$*`, unquoted, is subject to word splitting and globbing. +`$*` and `${array[*]}`, unquoted, is subject to word splitting and globbing. -Let's say you have three arguments: `baz`, `foo bar` and `*` +Let's say you have three arguments or array elements: `baz`, `foo bar` and `*` -`"$@"` will expand into exactly that: `baz`, `foo bar` and `*` +`"$@"` and `"${array[@]}" `will expand into exactly that: `baz`, `foo bar` and `*` -`$*` will expand into multiple other arguments: `baz`, `foo`, `bar`, `file.txt` and `otherfile.jpg` +`$*` and `${array[*]}` will expand into multiple other arguments: `baz`, `foo`, `bar`, `file.txt` and `otherfile.jpg` Since the latter is rarely expected or desired, ShellCheck warns about it.