Created
June 21, 2022 08:29
-
-
Save goddoe/0f4f893a3d96a8362e5c85353003caa4 to your computer and use it in GitHub Desktop.
array operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # for loop array | |
| # Reference: https://www.tutorialkart.com/bash-shell-scripting/bash-append-an-array-to-another-array/ | |
| array=( one two three ) | |
| for i in "${array[@]}" | |
| do | |
| echo "$i" | |
| done | |
| # append array | |
| # Reference: https://www.tutorialkart.com/bash-shell-scripting/bash-append-an-array-to-another-array/ | |
| arr1=("apple" "banana" "cherry") | |
| arr2=("mango" "grape") | |
| arr1+=(${arr2[@]}) | |
| echo ${arr1[@]} | |
| # join array | |
| # Reference: https://zaiste.net/posts/how-to-join-elements-of-array-bash/ | |
| function join { local IFS="$1"; shift; echo "$*"; } | |
| join , ${arr[@]} | |
| 1,a,2,b,c,d,e,3 | |
| result=$(join , ${arr[@]}) | |
| echo $result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment