Shell Scripting: Convert Uppercase to Lowercase

www.lautt‮c.iru‬om
Shell Scripting: Convert Uppercase to Lowercase

To convert uppercase characters to lowercase in a shell script, you can use the tr command with the -d and -s options.

For example, to convert a string stored in the FOO variable to lowercase, you can use the following command:

FOO=$(echo $FOO | tr '[:upper:]' '[:lower:]')

This will use the tr command to translate all uppercase characters in the FOO variable to lowercase characters and store the result in the FOO variable.

The -d option tells tr to delete characters that are specified in the first set but not in the second set. The -s option tells tr to replace multiple occurrences of a character with a single occurrence.

The [:upper:] and [:lower:] notation is used to specify character classes. The [:upper:] class includes all uppercase characters, and the [:lower:] class includes all lowercase characters.

You can also use the tr command to convert multiple strings at once. For example:

FOO=$(echo $FOO | tr '[:upper:]' '[:lower:]')
BAR=$(echo $BAR | tr '[:upper:]' '[:lower:]')
BAZ=$(echo $BAZ | tr '[:upper:]' '[:lower:]')

This will convert the FOO, BAR, and BAZ variables to lowercase.

Keep in mind that the tr command only works with ASCII characters. If you need to convert non-ASCII characters to lowercase, you may need to use a different approach, such as using the awk command or the sed command.

Created Time:2017-10-30 14:27:18  Author:lautturi