cshin9 cshin9 - 5 months ago 7
Bash Question

How to remove non-letters from a string in script?

I have:

something='sl89wS Gjf-_32b'


I want to return:

slwsgjfb


How can I make it return only letters and then make everything lowercase?

Answer

You can use Parameter Expansion:

${something//[^[:alpha:]]/}

here all non-alphabetic characters ([^[:alpha:]]) of variable something will be replaced by none i.e. will be omitted.

Example:

$ str='sl89wS Gjf-_32b'
$ echo "${str//[^[:alpha:]]/}"
slwSGjfb
Comments