I try to initialize an array in bash-4.2 next way:
ring=()
ls -las | tail -n +4 | while read line
> do
> ring+=("$line")
> echo ${ring[-1]}
> done
3924 -rw-r--r-- 1 username group 4015716 Mar 23 15:14 script.jar
4 -rw-r--r-- 1 username group 9 Feb 29 12:40 rec.lst
5541 -rw-r--r-- 1 username group 5674226917 Mar 28 15:25 debug.out
8 -rw-r--r-- 1 username group 6135 Mar 25 12:16 script.class
8 -rw-r--r-- 1 username group 6377 Mar 25 11:57 script.java
8 -rwxr-xr-x 1 username group 4930 Mar 8 15:21 script-0.0.0.sh
8 -rwxr-xr-x 1 username group 6361 Mar 28 15:27 script-0.0.1.sh
echo ${ring[0]}
echo "${ring[0]}"
echo "${ring[@]}"
What is wrong, why i get empty array after the finish of the loop?
echo $BASH_SUBSHELL
at various points might be instructive to see whether there is any subshell action going on - http://tldp.org/LDP/abs/html/internalvariables.html#BASHSUBSHELLREF – thrig Mar 28 '16 at 20:12