2 ms·
The article doesn't mention it, but very similar syntax is also used for arrays. For example: arr=(a b c) arr+=(d) ls "${arr[@]}" # ls "a" "b" "c"
by TheDong 7y ago
The article doesn't mention it, but very similar syntax is also used for arrays.
For example:
arr=(a b c)
arr+=(d)
ls "${arr[@]}" # ls "a" "b" "c" "d"
ls "${arr[*]}" # ls "a b c d"
This has quite nice symmetry with the fact that the 1st argument is "$1", and you replace the number with these symbols, and for arrays you access elements with "${arr[1]}", and again replace the number with the same symbols for the same behaviour.
If you do a lot of bash scripting, arrays are invaluable.
- useragent86 7y agoIndeed, and actual Bash scripting (as opposed to plain POSIX sh) is much more pleasant. Used properly, arrays make it easy to build commands with arguments and finally run them, e.g. #!/bin/bash command_args=( --foo bar --baz "buzz buzz" ) [[ $frob_option ]] && command_args+=(--frob frab) echo command_name "${command_args[@]}" "other" "arg" # Echoes: # command_name --foo bar --baz "buzz buzz" --frob frab other arg