Below, I have the demonstration of, and the listing of, functions in bash (4.2.46) for array manipulation - push, pop, shift, unshift. You'll notice that even though I pass the name of the array into the functions and assign it to the variable array_name
, the function only works with the hardcoded array array
. This is because I've been wraking my brain, my fingers, and the internet to no avail trying to determine the magic indirect incantation notation that would allow me to use array_name
instead, and be able to modify any array in these functions. Would anyone like to take a crack at it?
Thanks
UPDATE. This question was marked as as a duplicate. I used that question to solve my issue. And I repointed my shell to use Bash 4.3+.
Below now is the SOLUTION to my issue of using array variable indirection:
$ foo=
$ foo=()
$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
$ apush foo "item 1"
$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1
$ apush foo "item 2"
$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1
item 2
$ apush foo "item 3"
$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1
item 2
item 3
$ aunshift foo "item 0"
$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 0
item 1
item 2
item 3
$ apop foo
item 3
$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 0
item 1
item 2
$ ashift foo
item 0
$ echo '** foo array**' ; for i in "${foo[@]}"; do echo $i;done
** foo array**
item 1
item 2
## push, pop, shift, unshift:
## http://www.tech-recipes.com/rx/911/queue-and-stack-using-array/
## http://stackoverflow.com/questions/12691121/bash-array-expansion-using-variable-indirection-expansion
## http://stackoverflow.com/questions/23819839/assign-to-a-bash-array-variable-indirectly-by-dynamically-constructed-variable
##bash 4.3+: use declare -n to effectively create an alias ('nameref') of another variable
aclear ()
{
declare -n array_alias="$1"
array_alias=()
}
apush ()
{
declare -n array_alias="$1"
local item="$2"
local count=${#array_alias[@]}
## index = 0 to count-1. Next element is [count]
array_alias[$count]="$item"
}
alist ()
{
local use_idx=0
if [ "$1" = '--idx' ]
then
use_idx=1
shift
fi
declare -n array_alias="$1"
if [ "$use_idx" = 1 ]
then
for i in "${!array_alias[@]}"
do
printf "%s\t%s\n" "$i" "${array_alias[$i]}"
done
else
for i in "${array_alias[@]}"
do
echo $i
done
fi
}
apop ()
{
declare -n array_alias="$1"
local item=${array_alias[((${#array_alias[@]}-1))]}
array_alias=("${array_alias[@]:0:$((${#array_alias[@]}-1))}")
echo $item
}
ashift ()
{
declare -n array_alias="$1"
local item=${array_alias[0]}
array_alias=("${array_alias[@]:1}")
echo $item
}
aunshift ()
{
declare -n array_alias="$1"
local item="$2"
local i
local j
for (( i = ${#array_alias[@]} ; i > 0 ; i-- ))
do
((j=i-1))
array_alias[$i]="${array_alias[$j]}"
done
array_alias[0]="$item"
}
adelval ()
{
declare -n array_alias="$1"
local item=$2
local i
for (( i = 0 ; i < ${#array_alias[@]} ; i++ ))
do
if [ "$item" = "${array_alias[$i]}" ]
then
break
fi
done
adelidx $i
}
adelidx ()
{
declare -n array_alias="$1"
local idx=$2
array_alias=("${array_alias[@]:0:$idx}" "${array_alias[@]:$(($idx + 1))}")
}