From 4bd067d1d551bd38f071e2be0c7b21bed23ffc02 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 16 Jan 2014 15:27:59 -0500 Subject: bash-arrays: use unicode ellipsis instead of 3 periods --- public/bash-arrays.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/public/bash-arrays.md b/public/bash-arrays.md index b2c3d74..8720115 100644 --- a/public/bash-arrays.md +++ b/public/bash-arrays.md @@ -11,7 +11,7 @@ you don't need Bash arrays is what I like to call "wrong". I don't even mean real scripting; even these little stubs in `/usr/bin`: #!/bin/sh - java -jar /.../something.jar $* # WRONG! + java -jar /…/something.jar $* # WRONG! Command line arguments are exposed as an array, that little `$*` is accessing it, and is doing the wrong thing (for the lazy, the correct @@ -52,7 +52,7 @@ contains a space or newline. To help gain an intuitive understanding, I recommend using the following command to print a bullet list of words, to see how Bash splits them up: -
printf ' -> %s\n' words...
-> word one +
printf ' -> %s\n' words…
-> word one -> multiline word -> third word @@ -80,16 +80,16 @@ Normal array syntax - + - - + + - + - + @@ -276,7 +276,7 @@ anyway. - + @@ -339,7 +339,7 @@ I think possibly the only case where you do want word splitting with an array is when you didn't want an array, but it's what you get (arguments are, by necessity, an array). For example: - # Usage: path_ls PATH1 PATH2... + # Usage: path_ls PATH1 PATH2… # Description: # Takes any number of PATH-style values; that is, # colon-separated lists of directories, and prints a @@ -365,7 +365,7 @@ I still don't think I need arrays in my scripts Consider the common code: ARGS=' -f -q' - ... + … command $ARGS # unquoted variables are a bad code-smell anyway Here, `$ARGS` is field-separated by `$IFS`, which we are assuming has @@ -374,11 +374,11 @@ need an embedded space; which you do as long as it isn't based on anything outside of the program. But wait until you want to do this: ARGS=' -f -q' - ... + … if [[ -f "$filename" ]]; then ARGS+=" -F $filename" fi - ... + … command $ARGS Now you're hosed if `$filename` contains a space! More than just @@ -388,11 +388,11 @@ figures out how to make `filename='foo --dangerous-flag'`. Compare that with the array version: ARGS=(-f -q) - ... + … if [[ -f "$filename" ]]; then ARGS+=(-F "$filename") fi - ... + … command "${ARGS[@]}" What about compatability? -- cgit v1.2.3

Setting an array

-

words... is expanded and subject to word splitting +

words… is expanded and subject to word splitting based on $IFS.

array=(words...)array=(words…) Set the contents of the entire array.
array+=(words...)Appends words... to the end of the array.array+=(words…)Appends words… to the end of the array.
array[n]=word Sets an individual entry in the array, the first entry is at @@ -256,10 +256,10 @@ anyway.
Individual entries
${array[0]}$0
${array[1]}$1
...
${array[9]}$9
${array[10]}${10}
...
${array[n]}${n}
Subset arrays (array)
"${array[@]}""${@:0}"
Array length
${#array[@]}$# + 1
Setting the array
array=("${array[0]}" words...)set -- words...
array=("${array[0]}" words…)set -- words…
array=("${array[0]}" "${array[@]:2}")shift
array=("${array[0]}" "${array[@]:n+1}")shift n