[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
for loop over parameter expansion of array can miss resulted empty list
From: |
Alexey |
Subject: |
for loop over parameter expansion of array can miss resulted empty list |
Date: |
Mon, 21 Mar 2022 01:05:55 +0400 |
User-agent: |
Mail UserAgent |
Hello.
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -fstack-protector-strong -Wformat
-Werror=format-security -Wall
uname output: Linux alex 5.16.0-3-amd64 #1 SMP PREEMPT Debian 5.16.11-1
(2022-02-25) x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu
Bash Version: 5.1
Patch Level: 16
Release Status: release
Description:
If use 'Parameter Expansion', for example 'Remove matching suffix
pattern', on array and try to iterate over expansion result with for
loop, may occur that loop body will not be executed at all.
Repeat-By:
Code: x=("/"); for i in "${x[@]%/}"; do echo "i is '$i'"; done
Result: none
Expected result: i is ''
Expected behavior:
like for an array with empty element
Code: x=(""); for i in "${x[@]}"; do echo "i is '$i'"; done
Result: i is ''
another example, show that problems occurs only with empty resulted
list
Code: x=("/" "//"); for i in "${x[@]%/}"; do echo "i is '$i'"; done
Result: i is ''
i is '/'
Regards,
Alexey
- for loop over parameter expansion of array can miss resulted empty list,
Alexey <=
Re: for loop over parameter expansion of array can miss resulted empty list, L A Walsh, 2022/03/22