fbpx read Command in Linux: Capturing User Input in Scripts | Certificação Linux

read Command in Linux: Capturing Keyboard Input in Shell Scripts

📅 01/01/2019 ⏱ 2 min ✍️ Uira Ribeiro
Linuxcommands
read Command in Linux: Capturing Keyboard Input in Shell Scripts
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The read command in Linux is responsible for receiving data that users type via keyboard during script execution.

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

#!/bin/bashecho “Qual seu primeiro nome?”read primeironomeecho “Oi $primeironome... irei criar um diretório com seu nome”mkdir $primeironome

When running this script, it will ask for your name and create a directory as shown in the following example:

# ./criardiretorioQual seu primeiro nome?UiraOi Uira... irei criar um diretório com seu nome

See this other script example to determine if a year is a leap year (year with an extra day, February 29, in the Julian calendar).

#!/bin/bashecho "Digite o ano com quatro dígitos (ex.: 2019):"read anoif (( ("$ano" % 400) == "0" )) || (( ("$ano" % 4 == "0") && ("$ano" % 100 != "0") )); then echo "$ano é bissexto."else echo "$ano não é bissexto."fi

When running this script, it will ask which year you want to check if it's a leap year, as shown in the following example:

 ./anoDigite o ano com quatro dígitos (ex.: 2019):20192019 não é bissexto.

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

Liked it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp