bash

20220406080732-here-strings

bash

Here Strings # COMMAND <<< $WORD IRL example: [noaha@vbo-t3x-pro-nax: ~]$ while read -r ctid ips; do for ip in $ips; do echo vzctl set --ipdel $ip --save; echo vzctl set --ipadd $ip --save; done; done <<< $(echo "$vzlist") vzctl set --ipdel 199.250.221.227 --save vzctl set --ipadd 199.250.221.227 --save vzctl set --ipdel 199.250.222.59 --save vzctl set --ipadd 199.250.222.59 --save vzctl set --ipdel 70.39.249.21 --save vzctl set --ipadd 70.39.249.21 --save vzctl set --ipdel 199. ...

Arithmetic Expressions

bash

Number Bases # Shell supports numbers in any base. Notation Description number By default uses decimal (base 10) 0number In arithmetic expressions, numbers with a leading 0 are considered octal 0xnumber Hexadecimal notation base#number number is in base Specifying Different Number Bases

Array Indexing

bash

Bash supports two types of one-dimensional arrays. Multidimensional arrays aren’t supported. Indexed Arrays Associative Arrays Indexed Arrays # Indexed arrays use integer numbers as keys. Associative arrays (aka dictionaries) # Associative arrays use nonempty strings as keys. Referencing # Referencing array variables and their individual elements. Uses arrname[subscript] syntax. For indexed arrarys subscript is any valid arithmetic expression and for associative arrays subscript is a nonempty string. ...

Array Operations

bash

Outputting Contents # * and @ can be used to access every element in an array. [me@linuxbox ~]$ animals=("a dog" "a cat" "a fish") [me@linuxbox ~]$ for i in ${animals[*]}; do echo $i; done a dog a cat a fish [me@linuxbox ~]$ for i in ${animals[@]}; do echo $i; done a dog a cat a fish [me@linuxbox ~]$ for i in "${animals[*]}"; do echo $i; done a dog a cat a fish [me@linuxbox ~]$ for i in "${animals[@]}"; do echo $i; done a dog a cat a fish Number of array elements # [me@linuxbox ~]$ a[100]=foo [me@linuxbox ~]$ echo ${#a[@]} # number of array elements 1 [me@linuxbox ~]$ echo ${#a[100]} # length of element 100 3 Finding subscripts (indexes) # ${! ...

Arrays

bash

Regular variables are known in computer science as scalars. Arrays are organized like a table. A spreadsheet acts like a two-dimensional array. An individual array element is accessed using an address called an index or subscript. Most programming languages support multi-dimensional arrays. In Bash arrays are limited to a single dimension.

Assignment Operators

bash

Variables can be assigned within arithmetic expressions. Notation Description parameter = value Simple assignment. Assigns value to parameter. paramater += value parameter = parameter + value parameter -= value parameter = parameter - value parameter *= value parameter = parameter * value parameter /= value parameter = parameter / value parameter %= value parameter = parameter % value parameter++ Variable post-increment. ...

Associative Arrays

bash

Supported by bash 4.0+ Use strings rather than integers as array indexes. declare -A colors colors["red"]="#ff0000" colors["green"]="#00ff00" colors["blue"]="#0000ff" echo ${colors["blue"]}

Bash

bash, linux

Notes # Linux CLI Linux Utilities and Bash Builtins Bash Scripting Resources # Books # The Linux Command Line Linux Command Line and Shell Scripting Bible Websites # The Art of Command Line * Bash Hackers Wiki * Scripting Strict mode * For writing better shell scripts Shell Style Guide ShellCheck A script analysis tool. ...

Bash Expansion

bash, linux

Wildcards # Wildcards (aka Globbing or Globs) can be expanded by bash to match any of the following: Wildcard Meaning * Matches any characters ? Matches any single character [characters] Matches any character that is a member of the set characters [!characters] Matches any character that is not a member of the set characters [:class:] Matches any character that is a member of the specified class [:alnum:] matches any alphanumeric character [:alpha:] Matches any alphabetic character [:digit:] Matches any numeral [:lower:] Matches any lowercase letter [:upper:] Matches any uppercase letter Arithmetic Expansion # $((expression)) ...