Bash Scripting: Read input from command line

We can make a Bash script interactive by prompting a user for input. This can be done from the command line, with our script waiting for user input in order to proceed further.

The principal way to do this is via the read command. Although it is also possible to read input in the form of command line arguments that are passed to the Bash script when it is executed.

In this tutorial, you will learn how to read input from the command line with a Bash script and the read command.

In this tutorial you will learn:

  • How to read user input from command line
  • How to prompt and read input with same command
  • How to censor password entered from command line
Example of how to read input from the command line in a Bash script
Example of how to read input from the command line in a Bash script
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash Scripting: Read input from command line



  1. Let’s start with a simple example to see how the read command is used to prompt the user for input on the command line.
    #!/bin/bash
    
    echo "What is your name?"
    
    read name
    
    echo "Enjoy this tutorial, $name"

    The read command on line 5 will pause the script and wait for some input from the user. The input from the user will be stored in variable $name. After they enter their name, the script will continue to execute. Here is the output:

    $ ./test.sh 
    What is your name?
    linuxconfig
    Enjoy this tutorial, linuxconfig
    
  2. We do not necessarily need to use the echo command in the example above, as read is also capable of printing some text to the terminal. Let’s use the same example but without the first echo which asks the user their name.
    #!/bin/bash
    
    read -p "What is your name? " name
    
    echo "Enjoy this tutorial, $name"

    Notice that we needed to use the -p flag in order to make read generate some output to the terminal. As before, we are storing the user’s input in the $name variable. The result is the same as our first script, except we have prompted the user for input on the same line, instead of a new one.

    $ ./test.sh 
    What is your name? linuxconfig
    Enjoy this tutorial, linuxconfig
    
  3. It is also possible to read multiple words at once. See the following example where we ask for three words as input from the user.


    #!/bin/bash
    
    read -p "Enter three colors. " color1 color2 color3
    
    echo "You have entered: $color1 $color2 $color3"

    Here is the result of executing the script:

    $ ./test.sh 
    Enter three colors. red blue green
    You have entered: red blue green
    
  4. These examples have illustrated our point and shown you how to use read in order to handle user input. However, they are not practical examples and would be quite useless in the real world. Let’s try something that actually has some use. This script will test any year to see if it is a leap year.
    #!/bin/bash
    
    read -p "Enter a year: " year
    
    # check if the year is divisible by 4
    if (( $year % 4 == 0 )); then
        	echo "$year is a leap year"
    else
        	echo "$year is not a leap year"
    fi

    It is still a basic example, but at least it has some practical use. Note that we did not include any error checking so it is easy to glitch the script by entering letters and things like that.

    $ ./test.sh 
    Enter a year: 2021
    2021 is not a leap year
    
    $ ./test.sh 
    Enter a year: 2020
    2020 is a leap year
    
  5. The -s flag is another useful option to use with the read command. It is meant for prompting for passwords, so that the user’s input is not shown in the terminal.
    #!/bin/bash
    
    read -p "Enter username: " user
    read -sp "Enter password: " pass
    
    echo -e "\n\nGenerating user account for $user..."
    # do stuff

    Since we used the -s option, our password will not be shown in the terminal when we enter it.

    $ ./test.sh 
    Enter username: linuxconfig
    Enter password: 
    
    Generating user account for linuxconfig...
    

Closing Thoughts




In this tutorial, we learned how to read user input from the command line in a Bash script. This is facilitated by the read command, which is dead simple to use. You have seen in various examples how it can read input and store it as a variable for use later in the script.



Comments and Discussions
Linux Forum