https://www.tutorialspoint.com/unix/unix-io-redirections.htm

 

Unix / Linux - Shell Input/Output Redirections

Unix Linux Shell Input Output Redirections - In this chapter, we will discuss in detail about the Shell input/output redirections. Most Unix system commands take input from your terminal and send the resulting output back to your terminal. A command normal

www.tutorialspoint.com

Output Redirection

$ who > users
$ cat users
user	tty01	Sep 12 07:30
:

Example

$ echo line 1 > lines
$ cat lines
line 1
$

$ echo line 2 >> lines
$ cat lines
line 1
line 2
$

 

Input Redirection

The commands that normally take their input from the standard input can have their input redirected from a file in this manner.

For example, to count the number of lines in the file lines generated above,
you can execute the command as follows -

$ wc -l lines
2 lines
$

$ wc -l < lines
2
$

$ wc -l << EOF
> This is a simple lookup program
> for good (and bad) restaurants
> in Cape Town.
> EOF
3
$

 

Here Document

A here document is used to redirect input into an interactive shell script or program.

We can run an interactive program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script.

The general form for a here document is -

command << delimiter
document
delimiter

 

Here the shell interprets the << operator as an instruction to read input until it finds a line containing the specified delimiter. All the input lines up to the line containing the delimiter are then fed into the standard input of the command.

 

The delimiter tells the shell that the here document has completed.

Without it, the shell continues to read the input forever.

The delimiter must be a single word that does not contain spaces or tabs.

 

Follwing is the input to the command wc -l to count the total number of lines -

$ wc -l << EOF
> This is a simple lookup program
> for good (and bad) restaurants
> in Cape Town.
> EOF
3
$

You can use the here document to print multiple lines using your script as follows -

#!/bin/sh

cat << EOF
This is a simple lookup program 
for good (and bad) restaurants
in Cape Town.
EOF

Result

This is a simple lookup program
for good (and bad) restaurants
in Cape Town.

 

Discard the output

Sometimes you will need to execute a command, but you don't want the output displayed on the screen.

In such cases, you can discard the output by redirecting it to the file /dev/null -

$ command > /dev/null

The file /dev/null is a special file that automatically discards all its input.

 

To discard both output of a command and its error output,
use standard redirection to redirect STDERR to STDOUT -

$ command > /dev/null 2>&1

Here 2 represents STDERR and 1 represents STDOUT.

 

You can display a message on to STDERR by redirecting STDOUT into STDERR as follows

$ echo message 1>&2

 

Redirection Commands

Sequence Command Description
1 pgm > file Output of pgm is redirected to file
2 pgm >> file Output of pgm is appeded to file
3 pgm < file Program pgm read its input from file
4 n > file Output from stream with descriptor n redirected to file
5 n >> file Output from stream with descriptor n appended to file
6 n >& m Merges output from stream n with stream m
7 n <& m Merges input from stream n with stream m
8 << tag Standard input comes from here through next tag at the start of file
9 | Takes output from one program, or process, and sends it to another

the file descriptor

  • 0 = normally standard input (STDIN)
  • 1 = standard output (STDOUT)
  • 2 = standard error output (STDERR)

'OS > Linux' 카테고리의 다른 글

Linux Shell Script  (0) 2023.08.09
LD_LIBRARY_PATH  (0) 2022.12.21
CUDA 11.7.1 on WSL2  (0) 2022.11.13
Fastest way to check if a file exists  (0) 2022.11.10
Install libjpeg-turbo  (0) 2022.11.06

+ Recent posts