diff options
Diffstat (limited to 'public')
-rw-r--r-- | public/bash-arrays.md | 30 |
1 files 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: -<pre><code>printf ' -> %s\n' <var>words...</var><hr> -> word one +<pre><code>printf ' -> %s\n' <var>words…</var><hr> -> word one -> multiline word -> third word @@ -80,16 +80,16 @@ Normal array syntax <table> <caption> <h1>Setting an array</h1> - <p><var>words...</var> is expanded and subject to word splitting + <p><var>words…</var> is expanded and subject to word splitting based on <code>$IFS</code>.</p> </caption> <tbody> <tr> - <td><code>array=(<var>words...</var>)</code></td> + <td><code>array=(<var>words…</var>)</code></td> <td>Set the contents of the entire array.</td> </tr><tr> - <td><code>array+=(<var>words...</var>)</code></td> - <td>Appends <var>words...</var> to the end of the array.</td> + <td><code>array+=(<var>words…</var>)</code></td> + <td>Appends <var>words…</var> to the end of the array.</td> </tr><tr> <td><code>array[<var>n</var>]=<var>word</var></code></td> <td>Sets an individual entry in the array, the first entry is at @@ -256,10 +256,10 @@ anyway. <tr><th colspan=2>Individual entries</th></tr> <tr><td><code>${array[0]}</code></td><td><code>$0</code></td></tr> <tr><td><code>${array[1]}</code></td><td><code>$1</code></td></tr> - <tr><td colspan=2 style="text-align:center">...</td></tr> + <tr><td colspan=2 style="text-align:center">…</td></tr> <tr><td><code>${array[9]}</code></td><td><code>$9</code></td></tr> <tr><td><code>${array[10]}</code></td><td><code>${10}</code></td></tr> - <tr><td colspan=2 style="text-align:center">...</td></tr> + <tr><td colspan=2 style="text-align:center">…</td></tr> <tr><td><code>${array[<var>n</var>]}</code></td><td><code>${<var>n</var>}</code></td></tr> <tr><th colspan=2>Subset arrays (array)</th></tr> <tr><td><code>"${array[@]}"</code></td><td><code>"${@:0}"</code></td></tr> @@ -276,7 +276,7 @@ anyway. <tr><th colspan=2>Array length</th></tr> <tr><td><code>${#array[@]}</code></td><td><code>$#</code> + 1</td></tr> <tr><th colspan=2>Setting the array</th></tr> - <tr><td><code>array=("${array[0]}" <var>words...</var>)</code></td><td><code>set -- <var>words...</var></code></td></tr> + <tr><td><code>array=("${array[0]}" <var>words…</var>)</code></td><td><code>set -- <var>words…</var></code></td></tr> <tr><td><code>array=("${array[0]}" "${array[@]:2}")</code></td><td><code>shift</code></td></tr> <tr><td><code>array=("${array[0]}" "${array[@]:<var>n+1</var>}")</code></td><td><code>shift <var>n</var></code></td></tr> </tbody> @@ -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? |