This won't work as expected when there are whitespaces in the arrays:
function create_some_array() {
local -a a=()
for i in $(seq $1 $2); do
a[i]="$i $[$i*$i]"
done
echo ${a[@]}
}
and worse: if you try to get array indices from the outside "a", it turns out to be a scalar:
echo ${!a[@]}
even assignment as an array wont help, as possible quoting is naturally removed by the echo line and evaluation order cannot be manipulated to escape quoting: try
function create_some_array() {
...
echo "${a[@]}"
}
a=($(create_some_array 0 10))
echo ${!a[@]}
Still, printf seems not to help either:
function create_some_array() {
...
printf " \"%s\"" "${a[@]}"
}
seems to produce correct output on one hand:
$ create_some_array 0 3; echo
"0 0" "1 1" "2 4" "3 9"
but assignment doesn't work on the other:
$ b=($(create_some_array 0 3))
$ echo ${!b[@]}
0 1 2 3 4 5 6 7
So my last trick was to do assignment as follows:
$ eval b=("$(create_some_array 0 3)")
$ echo -e "${!b[@]}\n${b[3]}"
0 1 2 3
3 9
Tataaa!
P.S.: printf "%q " "${a[@]}" also works fine...
bash -x script_name
which will trace what is happening. – Tintorettomkdir tmp; touch tmp/{file1,file2}; ln tmp/file2 tmp/file3; bash yourscript tmp
outputs "The index number of .../tmp/file2 is 28578767 and its hard link is 2", "file2 is hardlinked to file3 with indexnumber: 28578767, file3 is hardlinked to file2 with indexnumber: 28578767". Seems to be working fine. – Groundnut