Here's an examplary hack, that bypasses assigning a temporary variable for chained parameter substitution:
declare -a values=( "4: " "#1" "#2" "#3" "#4" )declare -A arr["VALUES"]="${values[@]}"echo -e "Original: ${arr[@]}""${arr[@]#*: ?}" >&/dev/null; san="${_//#/}"echo -e "Sanitized: $san"
Explanation:
We initialize an associative array arr
with values from array values
.The first element contains the number of value elements, followed by a :
; this needs to be filtered out, as well as the prefix #
for all values.
"${arr[@]#*: ?}" >&/dev/null
This is a parameter substitution, that first expands the values of arr
and filters everything in front, up to and including '4: '
(#*: ?
), discarding the result, both of stdout
and stderr
. This also suppresses an error, too, due to the missing variable assigning of the result.
>&
is a short form of 2>&1
, assigning the stderr
output (fd2
) to stdout
(fd1
) and both to /dev/null
.
The trick now is, to use bash's special underscore _
parameter, which copies the discarded result parameter from the last command (1st parameter expansion operation above and result redirection to /dev/null
) into our second parameter substitution, where the #
value prefixes are filtered:
san="${_//#/}"
Output:
Original: 4: #1 #2 #3 #4
Sanitized: 1 2 3 4
Also see: Special parameters and shell variables