Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
1  
Could you verify the output in this post again? –  Ivan Chau Dec 10 '13 at 9:44

2 Answers 2

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
share|improve this answer
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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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