Linux Bash Scripting Tutorial
There are always 3 different files are open, stdin(keyboard), stdout(the screen) and stderr(error messages output to the screen) Readirection means caputuring output from a file, command, program, script or from code block. and sending it as output to another program,script or command. each open file gets a file descriptor
IO-Redirections mainly used for non-interactive operations.Standard Operations involve Reading input from keyboard,displaying output on a terminal and error messages also on terminal, Instead of that IO-Redirections reads data from File or Pipe or code block or another program, and outputs to File or Pipe or to a code block or another program
Operator | Meaning | Example | Description |
> | Output Redirection | ls > a.out | ls command output redirected to a.out |
>> | Output Redirection(append) | ls >> a.out | ls command output appended to a.out file |
< | Input Redirection | cat < file | cat command reads input from file |
$ cat < file
if file exists in the current directory,it reads line by line and echos on the terminal
if file doesnot exists in the current directory,error bash: file1: No such file or directory
$ ls > file
output of ls command writes into file.if file aleady exists in the current directory,then it truncates the file and writes the command output
if file doesnot exists in the current directory,creates a file specified in the IO redirection
$ ls >> file
output of ls command appends into a file.if file aleady exists in the current directory,then it appends command output to the existing file
if file doesnot exists in the current directory,creates a file specified in the IO redirection
within Shell script how to set a variable with file content
var1=`cat file.txt`
var2=`<file1`
Device File | File Descriptor | Abbrevation | Description |
/dev/stdin | 0 | stdin | /dev/stdin: symbolic link to /proc/self/fd/0 |
/dev/stdout | 1 | stdout | /dev/stdout: symbolic link to /proc/self/fd/1 |
/dev/stderr | 2 | stderr | /dev/stderr: symbolic link to /proc/self/fd/2 |
/dev/null |
for stdin default is keyboard
for stdout & stderr default is screen
Note: Redirection occurs before variable expansion,so variable containing redirection symbols must be expanded first using eval, otherwise, the redirection symbol is uninterpretted.
Output of one command redirected to standard input of another command.This technique is called as Pipe, denoted as | symbol. A command line includes tow or more commands connected with pipes is called as Pipeline
Pipeline syntax:
$ ~> command1|command2|command3|command4
Output of command1 becomes standard input of command2
output of command2 becomes input of command3
similarly output of command3 becomes input of command4.
choosing right command & combining them using a pipe technique is a programmers jab
$ ~> cat country.txtCountry Capital ISD_CODE USA Washington 1 China Beijing 86 Japan Tokyo 81 India Delhi 91$ ~> cat country.txt | wc -l
USA Washington 1 China Beijing 86 Japan Tokyo 81 India Delhi 91$ ~> cat country.txt | tail -n +2 | wc -l
Convert lower case letters to upper case letters using tr command.
$ ~> echo "hello world!" | tr a-z A-ZHELLO WORLD!
->Convert file content into upper case using tr command
$ ~> cat < sample.txt | tr [:lower:] [:upper:]
Find IP address of the system
$ ~> ifconfig wlp1s0 | grep 'inet'| head -n 1 | awk '{print $2}'output:
192.168.43.155
commamd | Description |
cmd >&n | Send output of cmd to file descriptor n |
cmd m>&n | Same as above, the output usually goes to file descriptor m is sent to file descriptor n instead |
cmd >&- | Close standard output |
cmd <&n | Take input for cmd from file descriptor n |
cmd m<&n | Same as previous one,normally input comes from file descriptor m comes from file descriptor n |
cmd <&- | Close standard input |
exec shell built-in command, executes commands in current process rather than launching new process.
In this program exec redirects standard input(usually keyboard keys) to file,the line it starts exec command to end of the file all read operations performed using this input-file
#!/usr/bin/bash #execd.sh exec <input-file read name address pin echo ${name} ${address} ${pin}
commamd | Description | ||
cmd << text | Here Document | ||
cmd <<< text | Here string | ||
cmd <> file | Open file for reading and writing | ||
cmd >| file | sends output of cmd to file even if the shell's noclobber is enabled/set | ||
Multiple Redirections | |||
cmd 2> file | send staderr to file;stdout remain same on the screen | ||
cmd > file 2>&1 | Send both stdout and stderr to same file | ||
cmd >& file | Send both stdout and stderr to same file | ||
cmd &> file | Send both stdout and stderr to same file(this is preferred form) | ||
cmd >file1 2>file2 | Send standard output to file1 and standard error to file2 |
noclobber prevent overwritting of files by redirection bash -C
ADS