FUNDAMENTALS A Complete Guide for Beginners
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:
❶ 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
➌ Next, write the following Bash script in the nano text editor:
#! /bin/bash
wc < input.txt
- #! /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
- 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
From 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.
Script (here.sh) >
#! /bin/bash
cat <<EOF > file.txt
Hello everyone!
How are you guys doing?
EOF
See 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.
First, let’s check out the contents of the file ‘input.txt’:
cat input.txt
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
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
From 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
Related Articles
- How to Read From Stdin in Bash? [4 Cases With Examples]
- What is Output Redirection in Bash [With 4 Practical Cases]
- How to Redirect Stderr in Bash [5 Practical Cases]
- How to Redirect Stdout and Stderr to File in Bash [5 Cases]
- Redirect Stderr to Stdout in Bash [5 Examples]
- 5 Ways to Echo to Stderr in Bash
<< Go Back to Bash Redirection | Bash Redirection and Piping | Bash Scripting Tutorial