Skip to main content
Add another pair of double quotes for `${#t[@]}`
Source Link
U. Windl
  • 1.8k
  • 15
  • 33

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
t=("${a[@]}")
declare -p a t
r=()
while [ $"${#t[@]}" -gt 0 ]
do
    r+=("${t[-1]}")
    unset 't[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ t=("${a[@]}")
+ declare -p a t
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a t=([0]="1" [1]="2" [2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 2 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 1 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
t=("${a[@]}")
declare -p a t
r=()
while [ ${#t[@]} -gt 0 ]
do
    r+=("${t[-1]}")
    unset 't[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ t=("${a[@]}")
+ declare -p a t
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a t=([0]="1" [1]="2" [2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 2 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 1 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
t=("${a[@]}")
declare -p a t
r=()
while [ "${#t[@]}" -gt 0 ]
do
    r+=("${t[-1]}")
    unset 't[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ t=("${a[@]}")
+ declare -p a t
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a t=([0]="1" [1]="2" [2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 2 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 1 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")
Add a pair of double-quotes around item to add to array
Source Link
U. Windl
  • 1.8k
  • 15
  • 33

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
t=("${a[@]}")
declare -p a t
r=()
while [ ${#t[@]} -gt 0 ]
do
    r+=($"${t[-1]}")
    unset 't[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ t=("${a[@]}")
+ declare -p a t
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a t=([0]="1" [1]="2" [2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=($"${t[-1]}")
+ unset 't[-1]'
+ '[' 2 -gt 0 ']'
+ r+=($"${t[-1]}")
+ unset 't[-1]'
+ '[' 1 -gt 0 ']'
+ r+=($"${t[-1]}")
+ unset 't[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
t=("${a[@]}")
declare -p a t
r=()
while [ ${#t[@]} -gt 0 ]
do
    r+=(${t[-1]})
    unset 't[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ t=("${a[@]}")
+ declare -p a t
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a t=([0]="1" [1]="2" [2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=(${t[-1]})
+ unset 't[-1]'
+ '[' 2 -gt 0 ']'
+ r+=(${t[-1]})
+ unset 't[-1]'
+ '[' 1 -gt 0 ']'
+ r+=(${t[-1]})
+ unset 't[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
t=("${a[@]}")
declare -p a t
r=()
while [ ${#t[@]} -gt 0 ]
do
    r+=("${t[-1]}")
    unset 't[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ t=("${a[@]}")
+ declare -p a t
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a t=([0]="1" [1]="2" [2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 2 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 1 -gt 0 ']'
+ r+=("${t[-1]}")
+ unset 't[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")
quote argument to `unset`, optimized for time vs.space, avoiding a level of indirection
Source Link
U. Windl
  • 1.8k
  • 15
  • 33

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
i=t=($"${!a[@]}")
declare -p a it
r=()
while [ ${#i[@]#t[@]} -gt 0 ]
do
    r+=(${a[${i[t[-1]}]})
    unset 'i['t[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ i=t=($"${!a[@]}")
+ declare -p a it
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a i=t=([0]="0"[0]="1" [1]="1"[1]="2" [2]="2"[2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=(${a[${i[t[-1]}]})
+ unset 'i['t[-1]'
+ '[' 2 -gt 0 ']'
+ r+=(${a[${i[t[-1]}]})
+ unset 'i['t[-1]'
+ '[' 1 -gt 0 ']'
+ r+=(${a[${i[t[-1]}]})
+ unset 'i['t[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
i=(${!a[@]})
declare -p a i
r=()
while [ ${#i[@]} -gt 0 ]
do
    r+=(${a[${i[-1]}]})
    unset 'i[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ i=(${!a[@]})
+ declare -p a i
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a i=([0]="0" [1]="1" [2]="2")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=(${a[${i[-1]}]})
+ unset 'i[-1]'
+ '[' 2 -gt 0 ']'
+ r+=(${a[${i[-1]}]})
+ unset 'i[-1]'
+ '[' 1 -gt 0 ']'
+ r+=(${a[${i[-1]}]})
+ unset 'i[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")

According to TIMTOWDI (There Is More Than One Way To Do It), here is my solution, reversing array a into r:

#!/bin/bash
set -u
a=(1 2 3)
t=("${a[@]}")
declare -p a t
r=()
while [ ${#t[@]} -gt 0 ]
do
    r+=(${t[-1]})
    unset 't[-1]'
done
echo "${r[@]}"
declare -p r

When executing with BASH "4.4.23(1)-release (x86_64-suse-linux-gnu)" I got:

+ set -u
+ a=(1 2 3)
+ t=("${a[@]}")
+ declare -p a t
declare -a a=([0]="1" [1]="2" [2]="3")
declare -a t=([0]="1" [1]="2" [2]="3")
+ r=()
+ '[' 3 -gt 0 ']'
+ r+=(${t[-1]})
+ unset 't[-1]'
+ '[' 2 -gt 0 ']'
+ r+=(${t[-1]})
+ unset 't[-1]'
+ '[' 1 -gt 0 ']'
+ r+=(${t[-1]})
+ unset 't[-1]'
+ '[' 0 -gt 0 ']'
+ echo 3 2 1
3 2 1
+ declare -p r
declare -a r=([0]="3" [1]="2" [2]="1")
quote argument to `unset`
Source Link
U. Windl
  • 1.8k
  • 15
  • 33
Loading
Source Link
U. Windl
  • 1.8k
  • 15
  • 33
Loading