Skip to content

Instantly share code, notes, and snippets.

@cinhtau
Last active February 11, 2022 13:18
Show Gist options
  • Save cinhtau/ea0bed82dfa8f996c3cd4149c2c31672 to your computer and use it in GitHub Desktop.
Save cinhtau/ea0bed82dfa8f996c3cd4149c2c31672 to your computer and use it in GitHub Desktop.
Working with Arrays in Linux Shell, bash and zsh, examples for https://blog.mimacom.com/arrays-on-linux-shell/
#!/usr/bin/zsh
echo "detecting shell:"
if [[ -n ${ZSH_VERSION} ]] && [[ ! -z ${ZSH_VERSION} ]]; then
INDEX_START=1
INDEX_OFFSET=0
echo " using zsh $ZSH_VERSION, index starts at $INDEX_START"
elif [[ -n $BASH_VERSION ]]; then
INDEX_START=0
INDEX_OFFSET=1
echo " using bash $BASH_VERSION, index starts at $INDEX_START"
else
echo " unknown"
fi
echo
# example data
my_array=(
"¯\_(ツ)_/¯"
"This"
"is"
"a"
"string"
"array"
"ᕙ(⇀‸↼‶)ᕗ"
)
# ${my_array[@]} - all of the items in the array
echo "array: ${my_array[@]}"
echo
# access item with index position
echo "index 5: ${my_array[5 - $INDEX_OFFSET]}"
echo
# ${#my_array[@]} - number of items in the array
echo "length: ${#my_array[@]}"
echo
# last element
if [[ -n $ZSH_VERSION ]]; then
echo "last index: ${my_array[-1]}"
elif [[ -n $BASH_VERSION ]]; then
# access last item, usually -1 works, on bash > v4.3
# mac os shell is v3,2
length=${#my_array[@]}
echo "last index: ${my_array[$length-$INDEX_OFFSET]}"
fi
echo
# ${#my_array[$INDEX_START]} - length of first item in array
echo "item: ${my_array[$INDEX_START]} has a length of ${#my_array[$INDEX_START]}"
echo
# ${my_array[@]} - iterate over values
echo "iterate over array"
for i in "${my_array[@]}"; do
echo " $i"
done
echo
echo "iterate with index"
if [[ -z $ZSH_VERSION ]]; then
# ${!my_array[@]} - all of the indexes in the array
for i in "${!my_array[@]}"; do
echo "index: ${i}, value: ${my_array[$i]}"
done
else
length=${#my_array[@]}
for i in $(seq 1 ${length}); do
echo "index: ${i}, value: ${my_array[$i]}"
done
fi
echo
#!/usr/bin/env bash
echo "detecting shell:"
if [[ -n ${ZSH_VERSION} ]] && [[ ! -z ${ZSH_VERSION} ]]; then
echo " using zsh $ZSH_VERSION"
elif [[ -n $BASH_VERSION ]]; then
echo " using bash $BASH_VERSION"
else
echo " unknown"
fi
echo
# example data
my_array=(
"¯\_(ツ)_/¯"
"This"
"is"
"a"
"string"
"array"
"ᕙ(⇀‸↼‶)ᕗ"
)
# ${my_array[@]} - all of the items in the array
echo "array: ${my_array[@]}"
echo
# access item with index position
echo "index 5: ${my_array[5]}"
echo
# ${#my_array[@]} - number of items in the array
echo "length: ${#my_array[@]}"
echo
# access last item, usually -1 works, on bash > v4.3
# mac os shell is v3,2
length=${#my_array[@]}
echo "last index: ${my_array[$length-1]}"
echo
# ${#my_array[0]} - length of item zero
echo "item: ${my_array[0]} has a length of ${#my_array[0]}"
echo
# ${my_array[@]} - iterate over values
echo "iterate over array"
for i in "${my_array[@]}"; do
echo " $i"
done
echo
# don't run this if zsh is running, version not set
if [[ -z $ZSH_VERSION ]]; then
# ${!my_array[@]} - all of the indexes in the array
echo "iterate with index"
for i in "${!my_array[@]}"; do
echo "index: ${i}, value: ${my_array[$i]}"
done
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment