Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace/remove literal square brackets in Bash string

I am looking for a solution to eliminate square brackets in a Bash string. For instance, consider the string:

ldr r3, [r0,#8]! 

However I am not sure how to eliminate the '[' and ']'. I would like to eliminate all symbols in the most elegant way possible, such as:

str="ldr r3, [r0,#8]!"
echo ${str//[,.!]/}

but with square brackets inclusive. How can this be accomplished?

like image 916
Anita Tino Avatar asked Nov 16 '25 02:11

Anita Tino


1 Answers

Another clean solution is tr:

tr: Translate, squeeze, and/or delete characters from standard input, writing to standard output.

where

-d, --delete delete characters in SET1, do not translate

With your example:

str="ldr r3, [r0,#8]!"
echo $str | tr -d "[]"

the output is:

ldr r3, r0,#8!
like image 118
Jose Avatar answered Nov 17 '25 22:11

Jose