A Guide To Using The Bash Read Command {10 Examples}
Introduction
The Bash read command is like having a conversation between your script and whoever is using it. It’s like your script asking a question and waiting for an answer. Let’s say you’re running a program or a script, and it needs some information to work properly. Instead of just doing its thing without any guidance, the read command allows your script to stop and ask the person using it for specific details. This is why we have termed the Bash read command a type of interaction. This article deals with the meaning of Bash script read syntax, options, and examples.
Bash read Command Syntax
Check out the basic Bash read command example below to get a deeper understanding of Bash read input.
The system will retain the “job position” while displaying the result of the command. In this way, there will be a conversation between the user and the script. Take a look at the result of the above syntax.
In this system, the output is generated based on the input provided by the user. For example, if the user inputs their job position as “doctor,” the system will address them as such. The user can input any detail, and the system will use it to personalize their experience.
Bash read Options
The following option helps the user alter the Bash read command. We have listed all the options with easy-to-understand explanations.
- -a <array>: It puts the words typed by the user into an array variable, which is like a container that holds multiple values in a single variable. For instance, if you type multiple words separated by spaces, it stores each word in a different position within the array. This type of arrangement works great for organizing and processing the data.
- -d <delimiter>: The read command in Bash stops processing when the user hits Enter. A “-d” option lets you specify a different character to stop reading the input. So, instead of entering, it stops when it encounters the specified character. For example, the user can specify “#” instead of “enter” to stop the read. This could be done in the case of newline characters that require a line break.
- -e: often associated with specific programs or utilities that allow for interactive shell sessions or other functionalities. It’s helpful for more complex interactions where too many actions are taken at once.
- -i <prefix>: Imagine you’re filling out a form online, and there’s already some text written in the box where you’re supposed to type. That’s like what “-i” does—it puts something there before you start typing. It’s helpful because it gives you an idea of what you might want to type or what’s expected of you.
- -n <number>: Using -n followed by a number limits the number of characters you can type. Once you’ve typed that many characters, it won’t let you type more unless you press the specified “Enter” or “Return” key to finish your input.
- -N <number>: The only difference between “-n” and -“N” is that the latter does not wait for the user to press the enter key at the end. It is more strict about word limits.
- -p <prompt>: The -p command lets you show a message or question to users before they type their input. It’s like a little message that tells you what you should type in.
- -r: Backslashes can change the meaning of certain characters, but with -r, they’re treated as regular characters. This is handy when you want to take in data exactly as it’s typed without any special interpretations.
- -s: Many times, passwords and other sensitive information are made silent. “-s” comes in handy in setting silent criteria on such information.
- -t <time>: Sets a time limit. If the user doesn’t input anything within the specified time in seconds, the command times out and moves on.
- -u <file descriptor>: Normally, read takes input from the keyboard (standard input). This option allows you to specify a different source, like a file or another input stream identified by its file descriptor number. This means you can read input from places other than the keyboard by specifying their identification numbers.
Also Read: How to Comment Effectively in Bash Scripting?
Bash read Examples
1. Arguments
Arguments alter the nature of the program. Look at the below example.
In the syntax above, the user’s response is retained after the argument “please enter your country” is prompted. For example, if the user responds with “Germany,” the result will be altered accordingly.
2. Piping
The main task of piping is to connect the output of one command to the input of another. This helps in refining the search in a short span of time. It’s like asking the computer to fill in the input rather than yourself.
3. Heredoc
A heredoc in shell scripting is like a text block you can directly put into a command or script without creating a separate file. It lets you input multiple lines of text easily, and it’s handy when you need to pass a bunch of words or a paragraph to a command without using external files. Check out the syntax to insert a block of text with the help of Heredoc.
4. Delimiters
Delimiters are commonly used to indicate the start or end of data sections or to separate different pieces of information. Go through the below example.
5. Prompt
As the name suggests, a “prompt” is a provocation from the program to type a command, provide information, or choose. Take a look at the example below.
6. Hide User Input
As mentioned above, the hidden user input is used to cover sensitive information. An example of the same is given below, in which the command invokes the “-s” option to silence the input.
7. Set Timeout
The following example is about a mathematical equation that needs to be solved in under 5 seconds. By including the “-t” option, the program can set a timeout for solving the equation.
Also Read: How To Use Grep Command In Linux/UNIX with Examples?
Conclusion
The article on the Bash read command provides a comprehensive overview of its functionalities, syntax, options, and examples. It explains how reading facilitates communication between a script and its user and delves into the essence of interactive scripting. The examples provided in the article demonstrate the versatility of the read command, showcasing its usefulness in different contexts. The article explains how to use arguments, piping, heredocs, delimiters, prompts, hiding user input, and setting timeouts to achieve specific functionalities, making the command adaptable to various scripting needs.