Array Operations
Indexing
Shell array uses 0-based indexing.
Basically, shell array indexing follows the syntax:
1 | {A[I]} # index a single element |
Let’s take a look at a few pratical use cases.
1 | A=(a b c) |
Concatenation
1 | B=(${A[@]} x) |
Loop
Iterate over a range
Python range
style iteration is supported since bash version 3.0.
1 | for i in {START..END} |
Bash 4.0+ has builtin support for setting up a step value:
1 | for i in {START..END..STEP} |
Expression based iteration
1 | for (( c=1; c<10; c++ )) |
An infinite loop
1 | for (( ; ; )) |