How to Convert Bash String to Lowercase? [7 Methods] | 您所在的位置:网站首页 › Loweecase › How to Convert Bash String to Lowercase? [7 Methods] |
![]() ![]() ![]() In Bash scripting, case conversion is important for case-sensitive comparison, pattern matching, and file and directory operations. So for an effective and efficient conversion of Bash string to lowercase, a clear concept of case conversion is essential. To convert a Bash string to lowercase, check the following methods: Using Parameter Expansion: ${String,,} Using “tr” command: tr [Option] [set1] [set2], here the tr command translates or deletes set1 with set2. Using declare command: declare -l variable_name Using awk command: echo "input_str" | awk 'pattern { action }' Using sed command: sed 's/pattern/replacement/' filename Using perl command: echo "$input_string" | perl [Option] 'action' Using ASCII value with a case statement and a for loop.Dive into the article to learn these methods of how to convert a bash string to lowercase in detail. Practice Files to Convert String To Lowercase in BashTable of Contents Toggle 7 Methods to Convert a String to LowercaseThe basic and simple way to convert a string’s characters to lowercase is by using the tr command. There are some other commands like awk, sed which gives flexibility while converting multibyte characters. In this section, 7 methods will be discussed which have been mentioned earlier on how to convert a string to lowercase. 1. Using Parameter Expansion With “,,” OperatorTo convert a string to lowercase, you can use parameter expansion. Parameter expansion refers to retrieving and manipulating the value that is stored in the parameter or variable. By using parameter expansion with,,,,, you can convert the first character of a string to lowercase, as well as convert all the characters in the string to lowercase. A. Convert All Characters to LowercaseTo convert all characters of a string to lowercase using,,, follow the below script: #!/bin/bash original_string="HELLO WORLD" lowercase_string="${original_string,,}" echo "Original String: $original_string" echo "Lowercase String: $lowercase_string" EXPLANATIONFirstly the script declares a string into a variable namedoriginal_string. Then the script uses a bash parameter expansion syntax${original_string,,} which converts all the characters of the original_string to lowercase. Later, the echo command prints the converted output in the terminal. From the output, you can see the script converted all the characters of the input string to lowercase. B. Convert the First Character to LowercaseTo convert only the first character to lowercase, replace the,,with ,in the code that you have used earlier to convert all characters to lowercase. Check the code: #!/bin/bash original_string="HELLO WORLD" lowercase_string="${original_string,}" echo "Original String: $original_string" echo "Lowercase String: $lowercase_string" EXPLANATIONThe "${original_string,}" converts the first character of the original_string to lowercase. Here the script only converts the first character of the string to lowercase. C. Convert a Specific Character Using PatternTo convert a specific character using a pattern, check the following code: #!/bin/bash # Declare a string string="HERE, ALL THE STRING IN UPPERCASE" # Converts the first letter to lowercase if it’s ‘H’ echo "${string,H}" # Convert all occurrences of 'L' to lowercase echo "${string,,L}" # Convert all occurrences of any of the characters in 'STRING' to lowercase echo "${string,,[STRING]}" EXPLANATIONFirstly the bash script declares a string named “string”. Here the ${string,H} checks if the first character of the string is H, then it will convert the found H to lowercase. After that, the ${string,,L} converts all the occurrences of the characterLto lowercase. Lastly, the ${string,,[STRING]} changes all the occurrences of any character in STRING to lowercase. The script successfully converts the first character, a specific character, and a group of characters to lowercase. Note: The use of ~ in parameter expansion allows you to switch the case of the first character, while ~~ switches the case of all characters. This results in a modification of the current character, alternating between lowercase and uppercase. 2. Using “tr” CommandTo convert the upper case character to a lower case character, you can use the tr command. In bash scripting, the tr command is used to translate and delete characters. Take a look at the following script to convert a string from upper case to lower case: #!/bin/bash Original_string="ALL THE CHARACTERS ARE IN UPPERCASE" lowercase_string=$(echo "$Original_string" | tr '[:upper:]' '[:lower:]') echo "Original String: $Original_string" echo "Lowercase String: $lowercase_string" EXPLANATIONAt first, the bash script declares a string in a variable namedOriginal_string. In the lowercase_string=$(echo "$Original_string" | tr '[:upper:]' '[:lower:]'), the | (pipe) symbol pipes the output of theechocommand to thetrcommand. Then the tr command matches the upper-case characters and replaces them with lowercase characters. From the output you can see, that the script converts the characters from upper case to lower case. 3. Using “declare” CommandWhile declaring a variable, if you use the -l attribute with declare command then it will convert any value of the declared variable to lowercase. To convert a string to lowercase using the declare command, check the following script: #!/bin/bash # Declare a string and convert it to lowercase declare -l str="CONVERTING THE STRING INTO LOWERCASE" # Print the string echo $str EXPLANATIONAt first, the bash script declares a string into a variable named str. Moreover, the script uses the -l option with the declare command which converts all the characters to lowercase. At last, the echo command prints the output of the str variable in the terminal. The output shows that the declare command has converted the characters to lowercase. 4. Using “awk’ CommandTo convert the case of a string to lowercase, you can use the awk command. The awk is a versatile command tool used for text processing, handling multi-byte characters, and especially for pattern matching and manipulation. The awk command uses a tolower function to change the case of the characters of a given string. Here’s the script to convert the character of a string to lowercase using the awk command: #!/bin/bash # Define the input string input_string="BASH LOWERCASE" # Use echo to print the original string and then pipe it to awk for lowercase conversion echo "$input_string" | awk '{print tolower($0)}' EXPLANATIONThe script uses |(pipe) operator to take the output of theechocommand to theawkcommand. In the awk '{print tolower($0)}', the awk command converts the echo command output to lowercase using its function tolower. In tolower($0), the ($0) indicates the entire output from the pipe operator. The script converts the characters to lowercase using the awk command with the tolower function. 5. Using “sed” CommandThe sed command is a simple yet very useful command for stream editors in bash scripting. The sed command uses a special sequence that includes backslashes and one of the following letters L, l, U, u, E. These sequences have a special meaning, like: \L: Continue the replacement to lowercase until you find \U or \E. \ l: Turn the next character to lowercase. \U: Continue the replacement to uppercase until you find \U or \E. \u: Turn the next character to uppercase. \E: Stoped case conversion started by \U and \L.The sed command mainly uses these sequences for case conversion. To convert a string to lowercase using the sed command, follow the script below: #!/bin/bash # Method 1 med1=$(echo 'Bash Lowercase with SED' | sed -e 's/\(.*\)/\L\1/') echo "Method1: $med1" # Method 2 med2=$(echo 'Bash Lowercase with SED' | sed 's/.*/\L&/g') echo "Method2: $med2" EXPLANATIONFirstly in method 1, thesedcommand takes the output through the pipe operator. In the sed command \(.*\) captures the string and the\Lconverts the strings’ characters to lowercase. Later in method 2, the script uses's/.*/\L&/g'which also converts the characters to lowercase. It uses-gflag which globally replaces all the characters in the output. From the output, you can see the inputs’ uppercase characters have converted to lowercase. Convert a Specific CharacterTo convert a specific character to lowercase using the sed command, take a look at the following script: #!/bin/bash Main_string="Hello World" New_string=$(echo "$Main_string" | sed -e 's/H/h/g' -e 's/W/w/g') echo "Main_string:" $Main_string echo "Modified_string:" $New_string EXPLANATIONIn the $(echo "$Main_string" | sed -e 's/H/h/g' -e 's/W/w/g'), the pipe operator takes the output of theechocommand and pipes it as input to the sed command. Thesed command replaces the characterHwithhandWwithwglobally. At last, the bash script stores the modified string in the New_string variable. The output shows that the script replaces the first character of the words from upper case to lower case. 6. Using “Perl” CommandIn bash scripting, the Perl command provides a simple way to convert strings to lowercase. The Perl command reads each line of the input then uses the lc function to convert the input to lowercase and later it displays the modified output. To convert a string to lowercase using the Perl command, check the following script: #!/bin/bash input_string="Bash Lowercase with Perl" echo "$input_string" | perl -ne 'print lc' EXPLANATIONIn theperl -necommand, the-noption makes the perl command process the input line by line and the -option executes the perl command in the terminal. Later, the 'print lc' syntax, prints the converted lowercase of the input characters using the lc function. The output shows the script converts the uppercase characters B, L, and P to lowercase b, l, and p respectively. 7. Using the ASCII ValueTo convert the case of a string to lowercase, you can also use the ASCII value of the characters and arithmetic operations. Here’s the script: #!/bin/bash lc(){ case "$1" in [A-Z]) n=$(printf "%d" "'$1") n=$((n+32)) printf \\$(printf "%o" "$n") ;; *) printf "%s" "$1" ;; esac } word="THIS IS a ASCII value." for((i=0;i |
How to Convert Bash String to Lowercase? [7 Methods]
CopyRight 2018-2019 实验室设备网 版权所有 |