0% found this document useful (0 votes)
23 views11 pages

Lab 7-3: Shell I/O Redirection Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views11 pages

Lab 7-3: Shell I/O Redirection Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Linux I/O Redirection

Redirection can be defined as changing the way from where commands read input to
where commands sends output. You can redirect input and output of a command.

For redirection, meta characters are used. Redirection can be into a file (shell meta
characters are angle brackets '<', '>') or a program ( shell meta characters
are pipesymbol '|').

Standard Streams In I/O Redirection


The bash shell has three standard streams in I/O redirection:

o standard input (stdin) : The stdin stream is numbered as stdin (0). The bash
shell takes input from stdin. By default, keyboard is used as input.
o standard output (stdout) : The stdout stream is numbered as stdout (1). The
bash shell sends output to stdout. Output goes to display.
o standard error (stderr) : The stderr stream is numbered as stderr (2). The
bash shell sends error message to stderr. Error message goes to display.

Redirection Into A File


Each stream uses redirection commands. Single bracket '>' or double bracket '>>'
can be used to redirect standard output. If the target file doesn't exist, a new file with
the same name will be created.

Overwrite

Commands with a single bracket '>' overwrite existing file content.

o > : standard output


o < : standard input
o 2> : standard error

Note: Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr you have to
write '2>'.
Syntax:

1. cat > <fileName>

Example:

1. cat > sample.txt

Look at the above snapshot, command "cat > sample.txt" has created 'sample.txt'
with content 'a, b, c'. Same file 'sample.txt' is created again with command "cat >
sample.txt" and this time it overwrites earlier file content and only displays 'd, e, f '.

Append
Commands with a double bracket '>>' do not overwrite the existing file content.

o >> - standard output


o << - standard input
o 2>> - standard error

Syntax:

1. cat >> <fileName>

Example:

1. cat >> sample.txt


Look at the above snapshot, here again we have created two files with the same
name using '>>' in command "cat >> sample.txt". But this time, content doesn't
overwrite and everything is displayed.

Redirection Into A Program


Pipe redirects a stream from one program to another. When pipe is used to send
standard output of one program to another program, first program's data will not be
displayed on the terminal, only the second program's data will be displayed.

Although the functionality of pipe may look similar to that of '>' and '>>' but has a
significance difference. Pipe redirects data from one program to another while
brackets are only used in redirection of files.

Example:

1. ls *.txt | cat > txtFile


Look at the above snapshot, command "ls *.txt | cat > txtFile" has put all the '.txt'
files into a newly created file 'txtFile'.

Linux Input Redirection


< stdin

The bash shell uses stdin to take input. In input redirection, a file is made input to the
command and this redirection is done with the help of '<' sign.

Syntax:

1. cat < <fileName>

Example:

1. cat < file.txt

Look at the above snapshot, command "cat < file.txt" has taken 'file.txt' as input
and displayed its content.

<< here document


The here document (sometimes also called here-is-document) is a way in which you
can enter input until a certain sequence(usually EOF) is typed. The EOF (End Of File)
can be typed or can be called by pressing (ctrl + d) keys.

Any word can be used in place of 'EOF' like we have used 'last'.

Syntax:

1. cat <<EOF> <fileName>


Example:

1. cat <<EOF> file.txt

Look at the above snapshot, in first example, 'file.txt' is ended when we


typed 'EOF' and 'last' in second example.

<<< here string

The here string is used to directly pass strings to a command.

Example:

1. base64 <<< format.txt

Look at the above snapshot, in this example we have used base64 which we'll study
later. Command "base64 <<< format.txt" has decoded file 'format.txt' and then by
using command 'base64 -d' we got back our file 'format.txt'.

Linux Output Redirection


Output redirection is used to put output of one command into a file or into another
command.
> stdout
The stdout is redirected with a '>' greater than sign. When shell meets the '>' sign, it
will clear the file (as you already know).

Example:

1. echo Hello everyone. > afile.txt

Look at the above snapshot, greater than sign '>' redirects the command 'echo'
output into a file 'afile.txt'.

Output File Is Erased


In output redirection, during scanning of a command line, shell will encounter
through '>' sign and will clear the file.

Example:

1. zcho Welcome > afile.txt

Look at the above snapshot, command "zcho Welcome > afile.txt" is wrong but
still file 'afile.txt' is cleared.

noclobber
We can prevent file deletion while using '>' sign with the help of noclobber option.

Syntax:

1. set -o noclobber (To prevent overwrite)


2. set +o noclobber (To overwrite)

Example:

1. echo Learn Linux. > newfile.txt

Look at the above snapshot, command "set -o noclobber" prevents file from getting
overwrite.

But command "set +o noclobber" allows you to overwrite the existing file.

Overruling noclobber
Overruling noclobber means you can overwrite an existing file while noclobber is set
by using '>|' sign.

Syntax:

1. command >| <fileName>

Example:

1. echo Welcome to JavaTpoint. >| newfile.txt


Look at the above snapshot, with greater than '>' sign, bash doesn't allow to
overwrite the file 'newfile.txt'. But with '>|' sign file is overwritten.

>>append
Append '>>' sign doesn't let the file content to be overwritten and hence, displays
new as well as old file content.

Syntax:

1. command >> <fileName>

Example:

1. echo You all are welcome here. >> newfile.txt

Look at the above snapshot, file 'newfile.txt' is not overwritten with append
command. New content is displyed with the old one.

Linux Error Redirection


2> stderr
Command '2>' redirects the error of an output.It helps us you to keep our display
less messy by redirecting error messages.

Example:

1. zcho hyii 2> /dev/null


Look at the above snapshot, by using command "zcho hyii 2> /dev/null" (here
echo command is wrong), we didn't get any error message. But when we use
command "zcho hyii" error message is displayed in the terminal. Hence, '2>'
redirects the error message in the mentioned directory keeping your terminal error
message free.

2>&1
This command helps in redirecting the stdout and stderr in the same file.

Example:

1. newfile.txt > abc.txt and error.txt 2>&1

Look at the above snapshot, 'abc.txt and error.txt' is directing to the same file
'newfile.txt'.

Note: Order of redirections is really important.

If you'll write:

1. ls > dirlist 2>&1

then, stdout and stderr both will be directed to the file dirlist.

But if you'll write:

1. ls 2>&1 > dirlist


then, only stdout will be redirected to dirlist. This is because, before the stdout is
redirected to dirlist, stderr has made a copy of stdout.

You might also like