I often print out the contents of an array using a quick printf
shorthand like this:
$ printf "%s\n" "${my_array[@]}"
$ printf "%s\0" "${my_array[@]}"
This works great in general:
$ my_array=(limburger stilton wensleydale)
$ printf "%s\n" "${my_array[@]}"
limburger
stilton
wensleydale
$
Works great, but when the array is empty it still outputs a single character (a newline or null byte):
$ my_array=()
$ printf "%s\n" "${my_array[@]}"
$
Of course, I can avoid that by testing first for an empty array:
$ [[ ${#my_array[@]} -gt 0 ]] && printf "%s\n" "${my_array[@]}"
$ (( ${#my_array[@]} )) && printf "%s\n" "${my_array[@]}"
But I use this idiom all the time and would prefer to keep it as short as possible. Is there a better (shorter) solution, maybe a different printf
format, that won't print anything at all with an empty array?
Bash's parameter expansion might help:
printf "%b" "${my_array[@]/%/\\n}"
From help printf
:
%b
: expand backslash escape sequences in the corresponding argument
or
printf "%s" "${my_array[@]/%/$'\n'}"
For what it is worth:
(( ${#my_array[@]} )) && printf "%s\n" "${my_array[@]}"
(( ${#my_array[@]} ))
is a Bash's stand-alone arithmetic expression that evaluates to true
when array length is greater than 0.
Anyway, when I need to debug the content of an array. I prefer to use:
declare -p my_array
It will clearly show details that a simple printf '%s\n'
cannot, like:
declare -a my_array=
or empty declare -a my_array=()
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With