How to convert a string to lowercase in Linux?

In a recent project, the string generated by uuid needs to be converted to lowercase.

So, we used the following Linux command to convert the string to lowercase.

Let’s use the awk, tr and sed commands to convert the string to lowercase.

Here I use the uuidgen command to generate uuid;

Example

In the following example, we use the uuidgen command to generate uuid, and then pipe the generated uuid as the input of the awk command and use the tolower function to convert lowercase.

➜  ~ uuidgen | awk '{print tolower($1)}'

tolower( String ) :

Returns the string specified by the String parameter, and each uppercase character in the string will be changed to lowercase. The mapping between uppercase and lowercase is defined by the LC_CTYPE category of the current locale.

In the following example, we also use the uuidgen command to generate uuid, and then use the tr command to convert the input uuid string into lowercase through the pipeline.

➜  ~  uuidgen  | tr '[:upper:]' '[:lower:]'

[:lower:] :  All lowercase letters

[:upper:] : All uppercase letters

In the following example, we use the sed regular to convert to lowercase strings.

➜  ~ uuidgen |sed -e 's/\(.*\)/\L\1/g'

Add a Comment

Your email address will not be published.