On 2022/03/21 03:40, Alex fxmbsw7 Ratchev wrote:
i solve this by shopt -s nullglob

Repeat-By:
   Code: x=("/"); for i in "${x[@]%/}"; do echo "i is '$i'"; done
   Result: none
   Expected result: i is ''
if you have nullglob set, then that is not the correct result.

I used:
#!/bin/bash
echo "x=${x[@]%/}"
for i in "${x[@]%/}"; do echo "i is <$i>"
done
----
I got:
/tmp/x
x=

To display what you want, use '*' for the array expansion.

#!/bin/bash
echo "x=${x[*]%/}"
for i in "${x[*]%/}"; do echo "i is <$i>"
done

this gives:
/tmp/x
x=
i is <>


Reason: "[@]"  displays each element in quotes.

If there are no elements, then it is a null list, as in ()
resulting in '0' loop runs.

if you use [*]...all elements are concatenated and the result is
quoted, thus you will always have 1 element and the loop will always be
run, at least, once.



Reply via email to