From cc1fd01731ac123ce345989576bcf3c8383a3741 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 14 May 2016 16:50:21 -0700 Subject: [PATCH] Created SC2179 (markdown) --- SC2179.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 SC2179.md diff --git a/SC2179.md b/SC2179.md new file mode 100644 index 0000000..5115b67 --- /dev/null +++ b/SC2179.md @@ -0,0 +1,28 @@ +## Use array+=("item") to append items to an array. + +### Problematic code: + +```sh +var=(one two) +var+=three +``` + +### Correct code: + +```sh +var=(one two) +var+=( three ) +``` +### Rationale: + +It looks like you are trying to append a string to an array with `var+=string`. This instead appends to the first element of the array (equivalent to `var[0]+=three`). + +In the problematic code, the array will therefore contain `onethree` `two`. + +Instead, append an array to the array with `var+=( elements )`. This will append the new items to the array. + +In the correct code, it will contain `one` `two` `three` as expected. + +### Exceptions: + +If ShellCheck mistakenly thinks the variable is an array when it's not (e.g. because the same name was used in a different context), you can ignore this error. \ No newline at end of file