Linux Input Redirection

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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 < <fileName>  

Linux Input Redirection1

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:

cat <<EOF> file.txt

Linux Input Redirection2

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

base64 <<< format.txt 

Linux Input Redirection3

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'.