Say I read some data into a Bash array:
$ IFS=" " read -a arr <<< "hello/how are/you iam/fine/yeah"
Now, I want to print the first /
-sliced field for each element in the array.
What I do is to loop over the elements and use shell parameter expansion to strip everything from the first /
:
$ for w in "${arr[@]}"; do echo "${w%%/*}"; done
hello
are
iam
However, since printf
allows us to print the whole content of the array in a single expression:
$ printf "%s\n" "${arr[@]}"
hello/how
are/you
iam/fine
... I wonder if there is a way to use the shell parameter expansion ${w%%/*}
at the time of using printf
, instead of looping over all the elements and doing it against every single one.