Computer scienceSystem administration and DevOpsCommand lineText processing

Getting data from standard input

5 minutes read

While writing bash scripts, it is beneficial to know about standard inputs of bash and how you can use them. Standard input in an interactive console refers to direct input from the keyboard. Bash allows you to have control over standard streams of input and output. That means that you can use file descriptors as input for the console.

So, let's look at how you can read text input and use perform various other tasks related to standard inputs.

Read command

Let's start with the simplest way of reading user input, using the read command. After you type it into your terminal, you'll be able to write something in the next line:

$ read
The input line will be here
$

Basically, read just reads the contents of STDIN. You can save the contents inside a variable by adding its name after the command. For example, here is a bash script called mario.sh:

#!/bin/bash

echo Write Mario

read mario

echo "It's me, $mario"

After launching this script in a terminal, the first output will be "Write Mario", after which you can enter your input. The next line will display a text with your input:

$ mario.sh
Write Mario
Mario
It's me, Mario
$

You can also improve this script with a prompt by using the -p option:

#!/bin/bash

read -p "Write Mario: " mario
echo "It's me, $mario"

The result is the same:

$ mario.sh
Write Mario: Mario
It's me, Mario
$

On top of reading input, Bash also allows you to save it in text files. Let's see how you can store input in files using operators.

Storing input in files with operators

You can create a new text file and save input text in it using the > operator. To do so, you'll just need to write the name of the file after the operator. After the command, an input line will appear where you can write some text. Let's write "First line". Here's an example using read and echo commands with the > operator:

$ read -p "Write something: " first
Write something: First line
$ echo $first > example.txt
$

First, use the read command to write something as an input. Then use echo to store your input with the > operator. This will automatically create a new file named example.txt which will contain the input. You can also add some new text to it by using the >> operator. So let's add "Second line" into example.txt:

$ read -p "Write another line: " second
Write another line: Second line
$ echo $second >> example.txt
$

When you try to check the contents of your file with the cat command, it should display both of your inputs:

$ cat example.txt
First line
Second line
$

When you use >, it replaces the contents of the file with the new input. So, if you want to keep the previous contents of your file, you should use >>.

But what if you want to fully erase the contents of the file without deleting the file itself? For this, you can use a null value with >:

$ > example.txt

Note that you can store error output using the 2 descriptor before operators: 2> and 2>>.

Taking input from files

These operators can be used with a variety of commands besides the ones that are mentioned. For example, let's try to write a simple bash script that will read input from your file with a while loop using <. There are two lines of text in the file — example.txt:

First line of input
Second line of input

Take a look at this simple script called while.sh that can take input from example.txt:

#!/bin/bash

filename='example.txt'
while read line; do
    echo "Your line:  $line"
done < "$filename"

First, the script stores the name of the file in a variable. Then, it uses a while loop to read lines inside the text file. At the end of the loop, the < operator and the filename variable indicate the file from which the input should be taken.

Executing this script gives the following output:

$ while.sh
Your line: First line of input
Your line: Second line of input
$

Similar to the standard input, you can use input from a file and, for example, store it inside another file. Let's modify the script to store the output in a new file. For this, use the >> operator.

filename='example.txt'
output='junior.txt'

while read line; do
    echo "Your line:  $line" >> junior.txt
done < "$filename"

When you execute this script and look at the contents of junior.txt, you will see that the results from the while loop are now stored there:

$ while.sh
$ cat junior.txt
Your line: First line of input
Your line: Second line of input
$

Conclusion

Let's summarize what you now know about data in standard input:

  • You can redirect standard streams using standard operators;
  • With combinations of different commands and operators, you can write scripts to use data from inputs as necessary.

Knowledge of how to get data from the input can help you improve your scripts. So you should learn more about operators and try out different combinations of commands with them.

22 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo