Linux string to lowercase

How to Convert a String to Lower Case in Bash?

Converting a string to lowercase can be helpful in various situations, such as comparing strings, searching strings, or formatting output. This is quite effective in creating a credential-based service on the system and, thus is a widely used practice in encryption.

This article will discuss various methods to convert a string to lowercase in Linux and provide examples for each method mentioned below.

  • Using the tr Command
  • Using the awk Command
  • Using the sed Command
  • Using the Bash String Manipulation
  • Using the Bash Built-in Function

Method 1: Using the tr Command

The tr command is a Linux command that translates or deletes characters from a string. A user can use the tr command to convert a string to lowercase by specifying the range of characters to be converted to lowercase. The syntax of the tr command is as follows:

echo "STRING" | tr '[:upper:]' '[:lower:]'

Here, the echo command is used to print the string, and the tr command converts the string to lowercase. The [:upper:] and [:lower:] are character classes that represent all uppercase and lowercase characters, respectively.

#!/bin/bash echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'

The output can be seen by executing the bash script below:

Method 2: Using the awk Command

The awk command is a Linux command used to manipulate and process text files. Users can use the awk command and the tolower() function to convert a string to lowercase. The tolower() function converts all uppercase characters to lowercase characters.

The syntax of the awk command is as follows:

Here, the echo command is used to print the string, and the awk command is used to convert the string to lowercase using the tolower() function.

#!/bin/bash echo "HELLO WORLD" | awk ''

The same output will be generated by executing this script which is mentioned below:

Method 3: Using the sed Command

The sed command is a Linux command that performs text transformations on an input file or stream. A user can use the sed command and s command to convert a string to lowercase. The s command replaces the matched pattern with the specified string.

The syntax of the sed command is as follows:

Here, the sed command is used to convert the string to lowercase using the s command, and the echo command will print the output. The \L is an escape character used to indicate the start of the lowercase conversion. The & is a special character used to represent the entire matched pattern.

#!/bin/bash echo "HELLO WORLD" | sed 's/.*/\L&/'

This code will generate the same output after execution as shown below:

Читайте также:  How to uninstall debian linux

Method 4: Using the Bash String Manipulation

Bash string manipulation is a feature of Bash that allows users to manipulate strings in various ways. They can use the Bash string manipulation to convert a string to lowercase using the $ syntax. The $ syntax converts all uppercase characters in the string to lowercase characters.

The syntax of the Bash string manipulation is as follows:

Here, the string variable is set to the string to be converted to lowercase, and the $ syntax is used to convert the string to lowercase.

#!/bin/bash string=»HELLO WORLD» echo $

Executing this bash script will generate the same output as shown below:

Method 5: Using the Bash Built-in Function

The Bash built-in function is a feature of Bash that provides various useful functions. A user can use the Bash built-in function to convert a string to lowercase using the declare -l variable declaration. The declare -l variable declaration makes the variable automatically converted to lowercase.

The syntax of the Bash built-in function is as follows:

syntax is used to declare the variable as a lowercase variable, and the echo $string command is used to print the lowercase string.

declare -l string="HELLO WORLD" echo $string

This will provide the same output as well while executing this bash script as shown below:

Conclusion

Converting a string to lowercase is important in certain situations, especially if that string is case-sensitive. In this article, 5 different methods have been discussed that can be utilized to convert any string to lowercase. These five methods can be implemented using the tr command, the awk command, the sed command, the bash string manipulation, and the bash built-in function.

Источник

Bash lowercase and uppercase strings

String data are used for different purposes in any bash commands or programming script. Sometimes we need to change the case of the string to get the desired output. The string can be converted to uppercase or lowercase. The string data is converted by using ‘tr’ command in the old version of bash. In this case, the keyword ‘: upper’ is used for uppercase and the keyword ‘: lower’ is used for lowercase. You can visit the following tutorial link to know more about the ‘tr’ command for converting the case of the string.

You can convert the case of the string more easily by using the new feature of Bash 4. ‘^’ symbol is used to convert the first character of any string to uppercase and ‘^^’ symbol is used to convert the whole string to the uppercase. ‘,’ symbol is used to convert the first character of the string to lowercase and ‘,,’ symbol is used to convert the whole string to the lowercase.

Читайте также:  Настройка kaspersky astra linux

Converting the case of the String

Example#1:

Run the following commands to assign a string input to the variable, $name, and the next commands are used to print the original value, print value by converting the first letter to uppercase and print value by converting all letters of the string to uppercase.

Example#2:

The following example shows how you can convert the first character of any string to uppercase by matching with a particular character. Here, the first character is compared with ‘l’ and ‘h’ by the last two commands.

Example#3:

In the following example, $language variable is used to store a text value and the third command is used to covert the first character of each word of the string to uppercase where the first character is ‘p’. The last command is used to match the first character of each word of the text with ‘p’ and ‘j’ and convert them to uppercase.

Example#4:

Create a base file named case1.sh with the following code. In this example, the user input is taken in the variable, $ans and the value of this variable is printed with other string by converting the first character to uppercase.

Example#5:

Create a bash file named case2.sh with the following code. The string value taken from the user is converted to uppercase and stored to the variable $answer. If the value of this variable matches to ‘ADD’ then the value of $a, and $b will be added and printed. If the value of this variable matched to ‘SUBTRACT’ then the subtraction result of $a, and $b will be printed. The script will print ‘Invalid answer’ if the value provided by the user doesn’t match with ‘ADD’ or ‘SUBTRACT’.

#!/bin/bash
a = 15
b = 20
read -p «Do you want to add or subtract? » ans
answer = $
if [ $answer == ‘ADD’ ] ; then
echo «The result of addition= $((a+b) )»
elif [ $answer == ‘SUBTRACT’ ] ; then
echo «The result of subtraction= $((a-b) )»
else
echo «Invalid answer»
fi

Example#6:

Create a bash file named case3.sh with the following script. In this example, a text value is taken from the user and stored into the variable $data. Next, the comma-separated character list is taken as input for the case conversion and stored into the variable $list. The variable is used to match the characters of the list with the value of $data. The script will print the output after converting the characters to uppercase where matches.

#!/bin/bash
read -p «Enter some text data: » data
read -p «Mention the letters with the comma that will convert to uppercase?: » list
echo -n «The highlighted text is : »
echo $

Example#7:

Create a bash file named case4.sh with the following code. Here, ,, operator is used to convert the values taken from the users and compare with the variable $username and $password. If both values match then the script will print “Valid user” otherwise it will print “Invalid user”.

Читайте также:  Linux посчитать объем файлов

#!/bin/bash
username = ‘admin’
password = ‘pop890’
read -p «Enter username: » u
read -p «Enter password: » p
user = $
pass = $
if [ $username == $user ] && [ $password == $pass ] ; then
echo «Valid User»
else
echo «Invalid User»
fi

Conclusion:

Hope, this tutorial will help you to learn the case conversion tasks in easier way by using the new feature of bash. For more information watch the video!

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How can I convert a string from uppercase to lowercase in Bash? [duplicate]

I have been searching to find a way to convert a string value from uppercase to lowercase. All the search results show approaches of using the tr command. The problem with the tr command is that I am able to get the result only when I use the command with the echo statement. For example:

y="HELLO" echo $y| tr '[:upper:]' '[:lower:]' 
y="HELLO" val=$y| tr '[:upper:]' '[:lower:]' string=$val world 

I needed to use val=$(echo $y | tr ‘[:upper:]’ ‘[:lower:]’) rather than just val=$y| tr ‘[:upper:]’ ‘[:lower:]’

7 Answers 7

If you are using Bash 4, you can use the following approach:

x="HELLO" echo $x # HELLO y=$ echo $y # hello z=$ echo $z # HELLO 

Use only one , or ^ to make the first letter lowercase or uppercase .

Thanks for your immediate response. When i tried the above it says $—bad substitution. Any how i tried another approach of y=»HI» val = $( tr ‘[A-Z]’ ‘[a-z]’

The ,, ^^ substitutions only work on bash 4, not bash 3 so you’d need to upgrade bash or use the tr approach.

If bad substitution message is returned, also check if you accidentally wrote ‘$’ before variable name ($ instead of $), which results in the same error as when bash version is too low (you can check it with ‘bash —version’).

I was confused for a moment because I was doing bash —version and getting 5.0.3, but then realized that was brew version of bash (at /usr/local/bin/bash ). My script had my system bash in the header with #!/bin/bash which was version 3 🙁

One way to implement your code is

y="HELLO" val=$(echo "$y" | tr '[:upper:]' '[:lower:]') string="$val world" 

This uses $(. ) notation to capture the output of the command in a variable. Note also the quotation marks around the string variable — you need them there to indicate that $val and world are a single thing to be assigned to string .

If you have Bash 4.0 or higher, a more efficient & elegant way to do it is to use Bash built-in string manipulation:

Источник

Оцените статью
Adblock
detector