diff options
Diffstat (limited to 'bash.md')
-rw-r--r-- | bash.md | 48 |
1 files changed, 47 insertions, 1 deletions
@@ -14,7 +14,7 @@ Remove longest from start # e.g. ${FILEPATH##*/} # get only file name portion -%% - longst from end, # - shortest from start +%% - longest from end, # - shortest from start ## Control flow @@ -53,3 +53,49 @@ Don't interpolate variables: cat << 'EOF' > file bla bla EOF + +## Arrays + +Declaring indexed arrays: + + declare -a name + +Associative arrays: + + declare -A name + +Assignment: + + array=([bla]=foo [ble]=bar...) + +All values: + + ${array[*]} # one word, elements separated with first character of $IFS + ${array[@]} # separate words when double quoted + +All keys: + + ${!array[*]} + ${!array[@]} + +## Commandline arguments + +* `$#`: number of arguments. + +## Getopts example + + while getopts 'a:b:c' flag; do + case "${flag}" in + a) do_something $OPTARG ;; + b) b_option=$OPTARG ;; + c) c_flag=1 ;; + *) error "Unexpected option ${flag}" ;; + esac + done + +### With positional arguments + +Positional arguments will have to be supplied after flag options (`command +[options] <args>`): + + shift $((OPTIND-1)) |