Skip to content

Read command in Linux (read data via keyboard)

The read command in Linux is responsible for receiving data that is entered by users via the keyboard during the execution of a script.

Imagine that you want to know the user’s name to create a directory with their name. See the following script:

#! /bin/bash <br></br>echo “What is your first name?”

Read first name
echo “Hi $first name… I will create a directory with your name”
mkdir $first name When running this script it will ask for your name and create a directory as in the following example:

#. /creardirectortorio <br></br>What is your first name?

Uira
Hi Uira… I’ll create a directory with your name See this other example script to find out if a year is a leap year (year with one more day, February 29th, in the Julian calendar).

#! /bin/bash <br></br>echo “Enter the four-digit year (e.g.: 2019):” <br></br>read year <br></br>if (((“$year”% 400) == “0")) || ((“$year”% 4 == “0") && (“$year”% 100! = “0"))); then <br></br>echo “$year is leap year.” <br></br>they <br></br>echo “$year is not a leap year.”

Fi When running this script, it will ask which year you want to know if it is leap, as in the following example:

. /year <br></br>Enter the four-digit year (e.g. 2019): 2019 <br></br><br></br>2019 is not a leap year.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share