Bash Input Redirection [3 Practical Cases]

In Bash, input redirection is a way to change the default input source for a command. Instead of reading input from the keyboard (Standard input or stdin), as an alternate way, you can also use input redirection where you can read input from a file or another command’s output. In this article, I will give a complete overview of how & when you can use bash input redirection with practical examples.

Key Takeaways

  • Getting familiar with the concept of input redirection.
  • Learning input redirection with practical bash script examples.

Free Downloads

What is Input Redirection?

Bash provides a way to interactively read input from the user or another command using standard input (stdin) which allows scripts and programs to accept data directly from the keyboard or other input sources. And, input redirection is a process where the standard input of a command is changed from the keyboard to a  file. By using the ‘<’ symbol you can take input data from a file instead of typing it interactively.

3 Practical Cases Bash Input Redirection

In the following article, I will show how you can use input redirection in different practical scenarios while generating Bash scripts.

Case 1: Redirecting Input From a File in Bash

You can redirect stdin for a command from a pre-created file using the input redirection procedure. Please follow the below steps to check the entire process yourself:

Steps to Follow >

❶ At first, open your Ubuntu Terminal application.

❷ Now, let’s check out the contents of the file ‘input.txt’ using the cat command:

cat input.txt

Text file content

➌ Next, write the following Bash script in the nano text editor:

#! /bin/bash

wc < input.txt

EXPLANATION
  • #! /bin/bash: shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash.
  • wc: Counts words, lines, characters & bytes of one or multiple files.
  • <: Input redirection operator.
  • txt: One of the text file names.

➍ Then, press CTRL+O to save the file named ‘wc.sh’ & press CTRL+X to exit the nano editor.

➎ After that, use the following command to make the script executable:

chmod u+x wc.sh

EXPLANATION
  • chmod: Changes the permission of files and directories.
  • u+x: Argument with chmod command to add the executable permission for the user.
  • sh: File which you want to make executable.

➏ Finally, run the script by the following command:

./wc.sh

Bash input redirection from a fileFrom the output image, you can see the script is showing line, word & character numbers of the file ‘input.txt’ that was redirected as stdin for the wc command.

Case 2: Redirecting Input Using “Here Documents” in Bash Scripts

A ‘Here document’ is another form of input redirection, which allows you to input directly within the script without the need for an external file. Check out the following script where I will redirect input while using here documents.

You can follow the Steps of Case 01, to save & make the script executable.

Script (here.sh) >

#! /bin/bash

cat <<EOF > file.txt
Hello everyone!
How are you guys doing?
EOF

EXPLANATION
The script starts with the cat command, which reads & displays the contents of a file. ‘<< EOF’ denotes the start of the here document. ‘EOF’ serves as a delimiter stating ‘End of File’. ‘> file.txt’ denotes that the output of the here document will be written to the file ‘file.txt’

Bash input redirection using here documentsSee from the image, the cat command is displaying the contents of the ‘file.txt’ file document.

Case 3: Combining Bash Input Redirection With Pipes

Along with Bash redirection, piping is an essential feature for managing the flow of data between commands and files. In this case, I will show how you can use the input redirection & piping operation in a single script.

You can follow the Steps of Case 01, to save & make the script executable.

First, let’s check out the contents of the file ‘input.txt’:

cat input.txt

Content of a text file

Now, write or simply copy the following script in the nano editor:

Script (pipe.sh) >

#! /bin/bash

# Input redirection from file ‘input.txt’
echo “Word frequency in ‘input.txt’: ”
cat < input.txt | tr -s ‘ ‘ ‘\n’ | sort | uniq -c

EXPLANATION
This script starts with the echo command to display a message. After that, the contents of the ‘input.txt’ file are redirected to the cat command to read. Then it uses piping (|) to use the output of the cat command as the stdin for the tr command. Which replaces all spaces with newlines (\n). This process splits the text into separate words.

Next, the sort command sorts the words in alphabetical order. And, finally, ‘uniq -c’ counts the occurrences of each word with the help of the uniq command.

Finally, run the script by the following command:

./pipe.sh

Bash input redirection with pipingFrom the image, you can see that the script outputs the counted occurrences of each word of the ‘input.txt’ file, as the output of the uniq command. This file was piped through as stdin to the tr command using the pipe operator ( | ).

Conclusion

To sum up, Bash input redirection is a valuable feature that allows you to manipulate input streams in various ways. Hope this article helps you understand the entire process of input redirection.

People Also Ask

What is 3 >& 1 in Linux?
In Linux, ‘3 >& 1’ is a command that tells the shell to redirect file descriptor 3 to file descriptor 1. This form of redirection is typically used in the context of Bash scripting.

What does 0 >& 1 do?
The command ‘0 >& 1’ in Linux redirects file descriptor 0 (Standard input) to file descriptor 1 (Standard output). This means that whatever would have been read from stdin will now be taken from the source from the same source as stdout.

How do I redirect a bash script?
To redirect the output of a Bash script, you can use the standard output redirection operators (‘>’, ‘>>’) and the standard error redirection operator (‘2>’). These operators allow you to redirect the output of your script to a file or to discard it entirely.

What does 2 >& 1 >/ Dev Null mean?
The command ‘2>&1 >/dev/null’ is a way to redirect both standard error (stderr-2) and standard output (stdout-1) to a special device file ‘/dev/null’ that discards any data written to it. It effectively suppresses any output that would have been printed to the terminal.

Related Articles


<< Go Back to Bash RedirectionBash Redirection and Piping | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment