Complete Guide to the Linux Shell Script read Command - Practical Tips from User Input to Reading Files Line by Line

Overview

This guide covers core practical use cases for the read command, which is essential for handling interactive user input and parsing text data in Linux Bash shell scripts. You will master everything from basic input handling to password masking (-s), timeout handling (-t), delimiter splitting (IFS), and the widely used line-by-line file processing pattern based on while loops.

1. Basics of the read Command, Prompt (-p), and Setting Default Values

Run the mkdir -p ~/read-demo && cd ~/read-demo command to create and navigate to a dedicated directory for practicing the read command.

mkdir -p ~/read-demo && cd ~/read-demo

The read command reads a single line from standard input and stores it in a variable. You can specify a prompt string with the -p option and assign a fallback default value when input is empty using the ${variable:-default} syntax.
In the execution result, because no input was directly passed to the read command, the username variable remained empty, resulting in the default value guest being printed according to ${username:-guest}.
⚙️ [Key Options]

-p : Specifies a prompt message to display on the screen before receiving input.
-s : Enables silent mode, which suppresses input characters from being displayed on the screen when accepting sensitive information such as passwords.
-t : Sets a timeout limit (in seconds) for waiting for input; terminates input handling if the timeout expires.
-n : Reads only the specified number of characters and terminates immediately without requiring the user to press Enter.
-r : Disables backslash (\) escape interpretation, reading input raw as-is.
-a : Splits words based on whitespace and stores them sequentially into an array variable.
-d : Specifies a custom delimiter character to terminate input instead of a newline.

bash -c ‘read -p “사용자 이름을 입력하세요: " username; echo “입력된 사용자: ${username:-guest}” «< “tester”’

2. Password Masking (-s) and Input Timeout (-t) Handling

Run the cd ~/read-demo command to navigate to the previously created ~/read-demo directory and continue the practice.

cd ~/read-demo

Input is received while masking the screen with the -s option and enforcing a 5-second timeout with the -t 5 option.
Since the password was passed successfully within the timeout limit, the exit status code ($?) returned 0.

bash -c ‘read -t 5 -sp “비밀번호를 입력하세요 (5초 제한): " userpass; echo; echo “입력 완료 여부: $?” «< “secret1234”’

3. Splitting into Multiple Variables and Arrays Using Delimiters (IFS) and the -a Option

Run the cd ~/read-demo command to navigate to the previously created ~/read-demo directory and continue the practice.

cd ~/read-demo

After setting the Internal Field Separator (IFS) to a comma (,), the read command reads a CSV-formatted string passed via a Here-String (<<<) and splits it into col1, col2, and col3 variables.
As seen in the execution result, the server name, IP address, and service name included in the string were properly split and assigned to each variable according to the specified delimiter.

bash -c ‘IFS=”,” read -r col1 col2 col3 «< “server01,192.168.1.10,nginx”; echo “서버: $col1 | IP: $col2 | 서비스: $col3”’

By setting the delimiter (IFS) to a colon (:) and using the -a option, a string formatted like /etc/passwd is split and stored into the user_info array variable.
The username (root) at index 0 and the login shell path (/bin/bash) at index 6 of the array were successfully extracted and printed.

bash -c ‘IFS=":" read -ra user_info «< “root:x:0:0:root:/root:/bin/bash”; echo “계정명: ${user_info[0]}, 셸: ${user_info[6]}”’

4. Practical Core: Line-by-Line Large File Processing Using while Loops and read -r

Run the cd ~/read-demo command to navigate to the previously created ~/read-demo directory and continue the practice.

cd ~/read-demo

Create servers.csv, a sample CSV file containing server names and operating statuses separated by commas, to practice line-by-line file processing.

echo -e “server-web,running\nserver-db,stopped\nserver-cache,running” > servers.csv

By combining a while loop with read -r and setting the Internal Field Separator (IFS) to a comma (,), each line of the servers.csv file is safely read line by line.
In the output result, each server’s name and status recorded in the file were successfully split and assigned to the name and status variables, and then printed in the specified format.

while IFS="," read -r name status; do echo “[상태 점검] $name 서버는 현재 $status 상태입니다.”; done < servers.csv

So far, we have covered the fundamentals of the read command in Linux shell scripts up to essential practical techniques for reading files line by line.
Try writing your own interactive automation scripts suited to your use cases using the various options and examples explored today.