Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell parameter expansion on arrays

Tags:

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.

like image 857
fedorqui 'SO stop harming' Avatar asked Jun 08 '16 09:06

fedorqui 'SO stop harming'


People also ask

How do you expand an array in bash?

${foo[1]} or ${asc_array[key1]} are referenced to a single array element. ${foo[@]} and ${foo[*]} expand to all members of the array foo . "${foo[@]}" expands each element to a single word, while "${foo[*]}" expands all elements to a single word.

What is parameter expansion?

Parameter expansion can be used to modify, expand or replace the value of the parameter. The optional braces are used with the variable when using variable parameter expansion, such as 'echo ${myvar}'.

How do you expand a bash shell variable?

Parameter expansion comes in many forms in bash, the simplest is just a dollar sign followed by a name, eg $a. This form merely substitutes the value of the variable in place of the parameter expansion expression. The variable name can also optionally be surround by braces, eg ${a}.

How does shell expansion work?

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words on these characters.


1 Answers

Oh, I just found the way: just use the parameter expansion normally, only that against ${arr[@]} instead of ${arr}!

$ IFS=" " read -a arr <<< "hello/how are/you iam/fine/yeah"
$ printf "%s\n" "${arr[@]%%/*}"
hello
are
iam

Greg's wiki helped here:

Parameter Expansion on Arrays

BASH arrays are remarkably flexible, because they are well integrated with the other shell expansions. Any parameter expansion that can be carried out on a scalar or individual array element can equally apply to an entire array or the set of positional parameters such that all members are expanded at once, possibly with an additional operation mapped across each element.

$ a=(alpha beta gamma)  # assign to our base array via compound assignment
$ echo "${a[@]#a}"      # chop 'a' from the beginning of every member
lpha beta gamma
$ echo "${a[@]%a}"      # from the end
alph bet gamm
$ echo "${a[@]//a/f}"   # substitution
flphf betf gfmmf
like image 194
fedorqui 'SO stop harming' Avatar answered Sep 28 '22 04:09

fedorqui 'SO stop harming'