Introduction
In task automation using shell scripts, you often need to interact with the user to request data, confirm actions or receive specific settings. The Bash read command allows you to capture the standard input from the keyboard and store it in variables, making scripts much more flexible and friendly. In this article we will explore your syntax, the most useful options and several practical examples that you can apply immediately in your own scripts.
Basic syntax
The simplest form of read is: read variable. This command stops the script running and expects the user to type a text line, ending with the Enter key. The input is stored in the indicated variable. If no variable is specified, the content is stored in the special variable REPLY. In addition, read supports several lags that modify your behavior, such as -p to show a prompt, -s to hide the input (useful for passwords), -t to set a waiting time, -n to limit the number of characters, -r to prevent the inverted bar from acting as an escape character and -a to read directly in an array.
Most commonly used options
- -p 'text': shows the text as a request message before reading the entry.
- -s: silent mode, does not show what the user type (ideal for passwords).
- -t seconds: sets a waiting time; if exceeded, read ends with error code.
- -n number: limits reading to that number of characters; after reaching them, read returns automatically.
- -r: inverted bar does not act as an escape character, it is read literally.
- -in the name of _ array: keep every word read in an element of the indicated array.
Practical examples
Simple reading with prompt
#! / bin / bashread -p 'Enter your name:' name $name!
Password reading
#! / bin / bashread -s -p'Enter your password: 'passwordechuecho' Password saved (in a real case it would have to hash). '
Reading with waiting time
#! / bin / bashif read -t 5 -p 'Continue? (s / n):' answer; then give answer: $answer the time it's gone, it's assumed 'n'. response = nfi
Reading in an array
#! / bin / bashread -a fruits -p 'Enter three fruits separated by spaces:' echo You have chosen: ${fruit [0]}, ${fruits [1]} and ${fruit [2]}
Process a file line by line
#! / bin / bashwhile IFS = read -r line; do echo Line read: $lineadone< archivo.txt
Error management and validation
It is important to check the read output code to detect situations such as spent waiting time or interruption by Ctrl + D. The special variable $? contains the state of the last command; a value other than zero indicates a failure. In addition, after reading you can validate the content using regular expressions or simple comparisons:
- if [-z $variable]]; then echo 'Empty input'; fi
- if! $variable =~ ^ [0-9] +$ ]]; then echo 'Not a number'; fi
Combine the -t option with a test of $? allows to offer a default value when the user does not respond in time.
Good practices
- Always use -r unless you need the inverted bar to work as an escape.
- Lock the variables in double quotes when using them to avoid problems with special spaces or characters.
- When you request sensitive information, it combines -s with -p and, after using the data, erases the variable if it is not necessary to keep it (variable unset).
- For readings that come from files, it combines IFS = and -r to preserve initial and final spaces.
- Documents the purpose of each read within the script with clear comments.
Conclusion
The read command is an essential tool for creating interactive and adaptable scripts in Linux. Dominating your syntax and your options allows you to request data safely, control waiting times, store input in arrays and manage errors properly. Applying the good practices described above, your scripts will be more professional, safe and easy to use for both you and other users.


