Let's say I read some data in 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 of the array.
What I do is iterate over the elements and use the shell parameter extension to remove everything from the first / :
$ for w in "${arr[@]}"; do echo "${w%%/*}"; done hello are iam
However, since printf allows us to print the entire contents 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 extension ${w%%/*} while using printf instead of iterating over all the elements and doing it against each individual.
source share