Basic Script Building – Redirecting Input and Output

Redirecting Input and Output

At times, you may find it beneficial to store the results of a command rather than simply viewing them on the screen. The bash shell offers various operators for redirecting command output to different destinations, such as files. Additionally, redirection can manage input, enabling you to redirect a file to a command for processing. This section will outline the steps required to implement redirection in shell scripts.

Output Redirection:

The most basic type of redirection is sending output from a command to a file. The bash shell uses the greater-than symbol (>) for this:

command > outputfile

Sometime, instead of of overwriting the file content,you may need to append output from a command to existing file.For Example, if you’re creating a log fileto document an action on the system.In this stituation, you can use the double greater-than symbol (>>) to append data: 

[root@engrabhishekroshan scripts]# date;who >> outputredirection.txt
Sun Apr 7 01:08:18 AM IST 2024
[root@engrabhishekroshan scripts]# cat outputredirection.txt
root pts/0 2024-04-06 18:06 (192.168.72.1)
[root@engrabhishekroshan scripts]#

Input Redirection:

Input redirection is the opposite of output redirection. Instead of taking the output of a command and redirecting it to a file, input redirection takes the content of a file and redirects it to a command.

Leave a Comment

error: Content is protected !!