1

If I have the following $STRING

aaa.bbb.ccc.[ddd].eee.fff.[ggg].hhh

is there any way, using bash parameter expansion, to echo the following

aaa.bbb..ccc.eee.fff..hhh

That is, remove all occurrences of square brackets and everything inside those brackets? Everything I've tried ends up either removing everything in the string after the first left bracket or removing the brackets but leaving behind everything inside the brackets.

1
  • 1
    Could you verify the output in this post again?
    – Ivan Chau
    Dec 10, 2013 at 9:44

2 Answers 2

1

Requires shopt -s extglob:

bash-4.1# STRING='aaa.bbb.ccc.[ddd].eee.fff.[ggg].hhh'

bash-4.1# echo "${STRING//\[+([^\]])\]}"
aaa.bbb.ccc..eee.fff..hhh
1
shopt -s extglob

STRING='[asd].aaa.bbb.ccc.[ddd].eee.fff.[ggg].hhh.[asd]'
printf '%s\n' "${STRING//@(.\[*([^]])]|\[*([^]])]?(.))/}"

would give:

aaa.bbb.ccc.eee.fff.hhh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .