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
    Could you verify the output in this post again? – Ivan Chau Dec 10 '13 at 9:44
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.