Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Linux Commands»Convert String to Lowercase in Bash – Easier Than You Think

    Convert String to Lowercase in Bash – Easier Than You Think

    By RahulAugust 1, 20221 Min Read

    Question: How do I convert all the characters to the lowercase of a string in the bash shell script?

    In Linux, the tr command is used to translate, squeeze, and/or delete characters. So with the help of the tr command, you can convert the case of any character. You can do this quickly with a single-line command.

    You can use the following command to convert a string to lowercase. Here the command takes the standard input of a string and processes it.

    echo "Input string here" | tr '[:upper:]' '[:lower:]' 
    

    Let’s discuss with an example.

    Example

    Let’s create a sample shell script. Initialize a variable with a string having a few uppercase letters. Then convert all the letters to lowercase and store them in another variable.

    1
    2
    3
    4
    5
    6
    7
    #!/usr/bin/env bash
     
    str="Hello World"
    lowerStr=$(echo "$str" | tr '[:upper:]' '[:lower:]')
     
    echo "Input String: $str"
    echo "Result String:  $lowerStr"

    Run the above script and see the results:

    Output:
    Input String: Hello World Result String: hello world

    You can see the result string has all the characters in lowercase.

    bash command lowercase string uppercase
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Capitalize the First Letter in a String Using JavaScript

    An Introduction to Bash Variables

    An Introduction to Bash Variables

    Bash LOCAL and GLOBAL Variables

    Bash LOCAL and GLOBAL Variables

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.