Skip to content

Instantly share code, notes, and snippets.

@goddoe
Created June 21, 2022 08:29
Show Gist options
  • Select an option

  • Save goddoe/0f4f893a3d96a8362e5c85353003caa4 to your computer and use it in GitHub Desktop.

Select an option

Save goddoe/0f4f893a3d96a8362e5c85353003caa4 to your computer and use it in GitHub Desktop.
array operations
# 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