You are on page 1of 35

Escaping and Quoting

• Escaping is providing a \ (backslash) before


the wildcard to remove its special meaning.
• Quoting is enclosing the wild-card within
quotes to remove its special meaning.
$ rm chap\*
// to remove file named "chap*" "\" is used to
suppress special meaning of *

$ cat chap0\[1-3\] // to list the contents of


the file named "chap0[1-3]"
Escaping space:
$ rm My\ Document.doc
// to remove file named "My Documend.doc"
Escaping \ itself:
$ echo \\ // outputs \
$ echo new line character is \\n
new line character is \n
Quoting
When a command argument is enclosed in
quotes, the meanings of all enclosed special
characters are turned off.
• $ rm ‘chap*’ // to remove file named
"chap*"
• $ rm “My Document.doc” // to remove
file named "My Documend.doc"
$echo “Command substiturtion uses`` while
Term is Evaluated $TERM”
Command substiturtion uses while Term is
Evaluated vt100
$echo ‘Command substiturtion uses`` while
Term is Evaluated $TERM’
Command substiturtion uses`` while Term is
Evaluated $TERM
Redirection : Three Standard Files

Terminal
• The shell associates three standard files with the
terminal:
• → two for display and
• → one for the keyboard.
• Our terminal is also represented by a specific
device name(/dev/tty)
• These files are actually of characters.

When a user logs in, the shell makes available
three standard files.
Standard input: The file representing input which is
connected to the keyboard.
Standard output: The file representing output which is
connected to the display.
Standard error: The file representing error messages
that come from the command or shell. This file is also
connected to the display.
Standard Input

• The standard input can represent three input


sources:
– The keyboard, the default source.
– A file using redirection with the < symbol.
– Another program using a pipeline.
By default, the shell directs standard input from the keyboard.
$wc hello world
<ctrl+d> // end of input
2 2 10 // output
The redirect input symbol (<) instructs the shell
to redirect a command’s input to come from the
specified file instead of from the keyboard.
$ cat sample.txt
Hello world
$ wc < sample.txt
2 2 10
How it works?
$ wc < sample.txt
1. On seeing the < , the shell opens the disk file,
sample.txt
2. It unplugs the standard input file from its
default source and assign it to sample.txt
3. Wc reads from standard input which has
earlier been reassigned by shell to sample.txt
Standard Output
The standard output can represent three possible
destinations:
– The terminal, the default destination.
– A file using the redirection symbols > and >>.
– As input to another program using a pipeline.
By default, the shell directs standard output from a command
to the screen.
• $ wc sample.txt > newfile
$ cat newfile
2 2 10
The redirect output symbol (>) instructs the
shell to redirect the output of a command to the
specified file instead of to the screen.
$ wc sample.txt > temp.txt
$ cat temp.txt
2 2 10
How it works?
wc sample.txt > newfile
1. on seeing > , the shell opens the disk file new
file.
2. It unplugs the standard output file from its
default destination and assign it to newfile
3. Wc opens file sample.txt for reading
4. Wc writes to standard output which has
earlier been reassigned by the shell to
newfile
Redirection is important in some cases

ls *.c > c_progs_all.txt


Three standard file is represented by number
called file descriptor.
0 for Standard input
1 for Standard output
2 for Standard error
0< 1> >2 same as <, >, >
Standard Error

• By default, the shell directs standard error


from a command to the screen.
Example:
$ cat empty.txt
cat: cannot open empty.txt
// error because empty.txt is non existent file
The redirect output symbol (>) instructs the
shell to redirect the error messages of a
command to the specified file instead of to the
screen.
• $ cat empty.txt >> errorfile.txt
• $ cat errorfile.txt
• cat: cannot open empty.txt
Filters: Using Both Standard Input and
Standard Output
Do all UNIX commands uses the features of
standard input and output?
Directory oriented commands like makdir etc and
file handling commands like cp..
Ls pwd who etc don’t read from standard input but
they write to standard output
lp read from standard input but don’t write to
standard output
Command like cat wc cmp gzip uses standard input
and standard output
Command like cat wc cmp gzip are called filters
$ cat calc.txt
(4*2)+2
$ bc < calc.txt > result.txt
$ cat result.txt
10
/dev/null and /dev/tty

Ex:
Cmp foo1 foo2 > /dev/null
• $who > user.txt
• $cat user.txt
Running 2 or more commands have following
disadvantages:
• The process is slow. The second command cant
act unless the first has completed its job.
• You require an intermediate file that has to be
removed after the first command has completed
its run. When handling large files, temporary files
can build up early and eat up disk space in no
time
PIPES
• Pipe is another form of output redirection.
• With piping, the output of a command can be
used as input (piped) to a subsequent command.
• Syntax:
$ command1 | command2
• Here, Output from command1 is piped into input
for command2.
• The symbol '|' denotes a pipe.
Example:
$ ls -l | lp
• Pipeline can be used with 2 or more
commands. But the user should know the
behavioral properties of each commands to
place them there.
• Example:
• $ls -l | wc -l | lp
// to print a count of files in current directory
When a Command Needs to be Ignorant
of its Source
• If we wish to find total size of all C programs
contained in the working directory, we can use
the command:
• $ wc –c *.c
• However, it also shows the usage for each file(size
of each file). We are not interested in individual
statistics.
• Solution: We must make wc ignorant of its input
source. So, feed the concatenated output stream
of all the .c files to wc –c as its input:
• $ cat *.c | wc –c
tee
• This is an external command that handles a
character stream by duplicating its input.
• This command
• → saves one copy in a file and
• → writes the other copy to standard output.
• This command is also a filter and hence can be
placed anywhere in a pipeline.
• Example:
• $ who | tee users.lst //to display the output of
who and save this output in a file users.lst
Command Substitution

• The shell enables one or more command arguments


to be obtained from the standard out put of another
command. This is called Command Substitution
• Ex date command
The date Today is Fri sep 20 19:10:16 IST 2019

Echo The date Today is `date` (` Backtick)


The date Today is Fri sep 20 19:10:16 IST 2019
We can use pipeline in command substitution
Ex:
$echo “there are `ls –l |wc –l ` files in current directory “
there are 58 files in current directory

$echo ‘there are `ls –l |wc –l ` files in current directory ‘

there are `ls –l |wc –l ` files in current directory


In Korn shell and bash shell
Echo The date Today is $(date)
shell variables
• Shell supports variables
• We already seen bulletin shell variable like
$PATH,$TERM etc…
• a variable assignment is of the form
variable=value
• Ex : count=5
• echo $count
• A variable can also assign value of another
variable
• Count=5
• Total=$count
• Variable name begin with a letter but can
contain numerals and _
• Case sensitive: x an X are different
• Shell variable is not typed language
• Shell variable is initialized to zero by default
• Shell variable can be removed with unset and
protected from reassignment by readonly
Effect of quoting and escaping
• Message =you\ didn\’t enter\ the\ filename
• Message =“you didn’t enter the filename”
$
Echo the avrage pay is \$1000
the avrage pay is $1000
Echo ‘the avrage pay is $1000’
Echo “the PATH is $PATH and current directory is is
`pwd`”
the PATH is /bin:/usr/bin and current directory is
home/kumar

Echo “the avrage pay is $1000”


the avrage pay is 000

$1 is Positional parameter
Where to use shell variable
• Setting a path:
• $ progs=‘/home/kumar/c_prog’
• Cd $progs; pwd
Command substitution:
Mydir=`pwd`;echo $mydir
Concatenating variables:

You might also like