[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: for loop over parameter expansion of array can miss resulted empty l
From: |
L A Walsh |
Subject: |
Re: for loop over parameter expansion of array can miss resulted empty list |
Date: |
Tue, 22 Mar 2022 13:53:55 -0700 |
User-agent: |
Thunderbird |
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.
Re: for loop over parameter expansion of array can miss resulted empty list,
L A Walsh <=
Re: for loop over parameter expansion of array can miss resulted empty list, L A Walsh, 2022/03/22
Re: for loop over parameter expansion of array can miss resulted empty list, Chet Ramey, 2022/03/24