1. Home
  2. Bash
  3. Bash Tutorial
  4. Bash – User Input

Bash – User Input

User Input In Bash

Linux read command is used for interactive user input in bash scripts. This is helpful for taking input from users at runtime.

Syntax:

read [options] variable_name

Example 1:

There is no need to specify any datatype for the variables with the read operation. This simply takes an input of any data type values. Try with a sample command. Open bash shell terminal and type following command.

read myvar

The above command will wait for user input. so just type anything and press enter. Let’s create a simple shell script to read your name and print.

#!/bin/bash

echo "Enter your name:"
read myname
echo "Hello" $myname

Example 2:

Let’s know some more options used with the read command. For example to prompt some message with the read command.

read -p "Enter your username: " myname

Use -s to input without displaying on the screen. This is helpful to take password input in the script.

read -sp "Enter your password: " mypassword

Now, put both commands in a shell script and execute.

Tags , , ,