Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash read Command - DesignLinux

designlinux 0 Comments

Bash ships with a number of built-in commands that you can use on the command line or in your shell scripts.

In this article, we’ll explore the built-in read command.

Bash read Built-in #

read is a bash built-in command that reads a line from the standard input (or from the file descriptor) and split the line into words. The first word is assigned to the first name, the second one to the second name, and so on.

The general syntax of the read built-in takes the following form:

read [options] [name...]

To illustrate how the command works, open your terminal, type read var1 var2, and hit “Enter”. The command will wait for the user to enter the input. Type two words and press “Enter”.

read var1 var2Hello, World!

The words are assigned to the names that are passed to the read command as arguments. Use echo or printf to verify it:

echo $var1echo $var2
Hello,
World!

Instead of typing on the terminal, you can pass the standard input to read using other methods such as piping, here-string, or heredoc :

echo "Hello, World!" | (read var1 var2; echo -e "$var1 \n$var2")
read and echo are enclosed in parentheses and executed in the same subshell.
Hello,
World!

Here is an example using a here string and printf:

read -r var1 var2 <<< "Hello, World!"printf "var1: %s \nvar2: %s\n" "$var1" "$var2"
Var1: Hello, 
Var2: World! 

When no argument is provided to the read command, the entire line is assigned to the REPLY variable:

echo "Hello, world!" | (read; echo "$REPLY")
Hello, World!

If the number of arguments supplied to read is bigger than the number of words read from the input, the remaining words are assigned to the last name:

echo "Linux is awesome." | (read var1 var2; echo -e "Var1: $var1 \nVar2: $var2")
Var1: Linux 
Var2: is awesome.

Otherwise, if the number of arguments is less than the number of names, an empty value is assigned to the remaining names:

echo "Hello, World!" | (read var1 var2 var3; echo -e "Var1: $var1 \nVar2: $var2 \nVar3: $var3")
Var1: Hello, 
Var2: World! 
Var3: 

By default, read interprets the backslash as an escape character, which sometimes may cause unexpected behavior. To disable backslash escaping, invoke the command with the -r option.

Below is an example showing how read works when invoked with and without the -r option:

read <<< "Hello, \tWorld!"printf %s "$REPLY"
Hello, tWorld!
read -r <<< "Hello, \tWorld!"printf %s "$REPLY"
Hello, \tWorld!

Generally, you should always use read with the -r option.

Changing the Delimiter #

The default behavior of read is to split the line into words using one or more spaces, tabs, and newline as delimiters. To use another character as a delimiter, assign it to the IFS variable (Internal Field Separator).

echo "Linux:is:awesome." | (IFS=":" read -r var1 var2 var3; echo -e "$var1 \n$var2 \n$var3")
Linux 
is 
awesome.

When IFS is set to a character other than space or tab, the words are separated by exactly one character:

echo "Linux::is:awesome." | \  (IFS=":" read -r var1 var2 var3 var4; echo -e "Var1: $var1 \nVar2: $var2 \nVar3: $var3 \nVar4: $var4")

The line is separated in four words. The second word is an empty value representing the segment between the delimiters. It is created because we used two delimiter characters next to each other(::).

Var1: Linux 
Var2:  
Var3: is 
Var4: awesome.

You can use more than one delimiter to split the line. When specifying multiple delimiters, assign the characters to the IFS variable without a space between them.

Here is an example using _ an - as delimiters:

echo 'Linux_is-awesome.' | (IFS="-_" read -r var1 var2 var3; echo -e "$var1 \n$var2 \n$var3")
Linux 
is 
awesome.

Prompt String #

When writing interactive bash scripts, you can use the read command to get the user input.

To specify a prompt string, use the -p option. The prompt is printed before the read is executed and doesn’t include a newline.

Here is a simple example:

read -r -p "Are you sure?"

Generally, you would use the read command inside a while loop to force the user to give one of the expected answers.

The code below will prompt the user for system reboot :

while true; do
    read -r -p "Do you wish to reboot the system? (Y/N): " answer
    case $answer in
        [Yy]* ) reboot; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer Y or N.";;
    esac
done

If the shell script asks users to enter sensitive information, like password, use the -s option that tells read not to print the input on the terminal:

read -r -s -p "Enter your password: "

Assign the Words to Array #

To assign the words to an array instead of variable names, invoke the read command with the -a option:

read -r -a MY_ARR <<< "Linux is awesome."

for i in "${MY_ARR[@]}"; do 
  echo "$i"
done
Linux 
is 
awesome.

When both an array and a variable name are given, all words are assigned to the array.

Conclusion #

The read command is used to split a line of input into words.

If you have any questions or feedback, feel free to leave a comment.

bash terminal

Related

Tags: bash, terminal

How to Convert PDF to Image Using Gimp

Prev Post

How to Install GIMP 2.10 on CentOS 8

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 606
  • 1,055,378

DesignLinux.com © All rights reserved

Go to mobile version