Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid printing anything in Bash `printf` with an empty array?

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?

like image 268
Sasgorilla Avatar asked Dec 22 '22 15:12

Sasgorilla


2 Answers

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'}"
like image 187
Cyrus Avatar answered Dec 28 '22 07:12

Cyrus


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:

  • element key or index,
  • empty elements,
  • elements containing non-printable characters,
  • all of variable type or flags,
  • if array is undeclared, void declare -a my_array= or empty declare -a my_array=().
like image 39
Léa Gris Avatar answered Dec 28 '22 06:12

Léa Gris