tr
command in Linux
can be used to trim special characters from a string in bash script. Let's say we want to remove underscore (_) from the string _hello_
:$ echo "_hello_" | tr -d "_"As you can see the output is
hello
hello
.-d
flag causes to delete _
from the whole string.In case you want to delete multiple special characters, that's easy as pie:
$ echo "_sal-am_" | tr -d "_-"
salam
We have removed - and _ from the string above.
Making lower case to uppercase:
tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
asdasd
ASDASD
OR:
tr [:lower:] [:upper:]
asdasd
ASDASD
Turn {} into ():
$ echo '{hello}' | tr '{}' '()'
(hello)
#linux #tr #translate #trim #bash #sysadmin