Meta Characters
INTRODUCTION & CLASSIFICATION
- Shell metacharacters are characters that are handled specially by the shell.
- When you enter a command, the shell scans it for metacharacters and processes them specially.
- When all metacharacters have been processed, the command is finally executed.
| Classification of Metacharacters | |
|
Group Category |
Symbol or Usage |
| Redirection & pipes | > >> < << | |
| Wildcards | * [] ? ! {} |
| Quoting , Escaping & dereferencing | ‘ ` “ \ $ |
| Command Substitution | `command` |
| Command sequencing | Command1 ; command2 ; command3 |
| Grouping Commands | (Command1 ; command2 ; command3) {; ;} |
| Conditional Execution | || && |
| Background Process | & |
| Comment a line | # |
| To end a command line | Newline |
| To separate element on a command line | SPACE or TAB |
REDIRECTION & PIPES
| SHELL REDIRECTION & PIPES | |
|
SYMBOL |
MEANING |
| < | Input redirection;
Reads standard input from a file. |
| > | Output redirection;
Writes standard output to a file. |
| >> | Output redirection;
Appends standard output to a file. |
| | | Pipe symbol;
Sends the output of one process to the input of another. |
| SHELL REDIRECTION & PIPES |
| The concept of redirection is simply that of redirecting our input or output to something other than the default. For example, the standard output of most commands will be redirected to the shell window.
ls -l
|
| We can redirect the output of a command to a file using the > output redirection symbol.
ls -l > out.txt
What would have been emitted to the shell window will now be present in the output file out.txt |
| To append the output of a command to an existing file, using the >> output redirection symbol.
ls -l >> out.txt
The output of the above command is appended to the file out.txt |
| Rather than accept input from the keyboard, we can accept input from another source. For example, the command cat will simply emit its standard input (or files named on its command line) to its standard out.
cat out.txt
|
We can redirect the contents of a file to the cat using the < input redirection symbol.
cat < out.txt
|
| We can also build more complicated redirection structures. For example, using the | pipe symbol, we can chain a number of generators and filters together.
cat out.txt | wc
|
| Standard In/Out/Error |
| For each application, three special file descriptors are automatically created. These are called standard-input, standard-output, and standard-error. |
![]()
|
| • The stdin descriptor is generally the keyboard.
• Descriptors stdout and stderr are the terminal or window attached to the shell (stdout for program results, stderr for program errors, warnings, and status). • While stdout and stderr share the same default output device, they can be split as desired by the developer. |
| • Recall from our previous discussion that we can redirect stdout to a file as:
prog > out.txt
• The output of prog would be redirected to the file out.txt. Note that if we wanted to append our output to out.txt rather than replace the file altogether, we would use the double redirect, as:
prog >> out.txt
|
| • We could redirect only the error output as:
prog 2> error-out.txt
• If instead, we wanted to redirect both the stdout to a file (out.txt) and stderr to a file (err.txt) :
prog 1> out.txt 2>err.txt
|
| File Descriptors for Standard I/O Descriptors | |
|
Descriptor |
Description |
| 0 | Standard input (stdin) |
| 1 | Standard output (stdout) |
| 2 | Standard error (stderr) |
Basically you can:
|
|
WILDCARDS
| FILENAME SUBSTITUTION( WILDCARDS ) | |
| All shells support a wildcard facility that allows you to select files that satisfy a particular name pattern from the file system. | |
|
Wildcard |
Meaning |
| * | Matches any string, including the empty string. |
| ? | Matches any single character. |
| [] | Matches any one of the characters between the brackets. A range of characters may be specified by separating a pair of characters by a hyphen |
| ! | Don’t include or Negate |
| Examples |
ls -FR a.c b.c cc.c dir1/ dir2/ dir1: d.c e.e dir2: f.d g.c
recursively list the current directory. |
ls *.c a.c b.c cc.c
list any text ending in “.c” |
| Examples |
ls ?.c a.c b.c
list text for which one character is followed by “.c” |
ls [ac]* a.c cc.c
list any string beginning with “a” or “c” |
ls [A-Za-z]* a.c b.c cc.c
list any string beginning with a letter.
|
| Examples |
ls dir*/*.c dir1/d.c dir2/g.c list all files ending in “.c” files in “dir*”
directories ( that is, in any directories beginning with “dir” ). |
ls */*.c dir1/d.c dir2/g.c
list all files ending in “.c” in any subdirectory.
|
ls *2/?.? ?.? a.c b.c dir2/f.d dir2/g.c
list all files with extensions in “2*” directories and current directory. |
Quoting,Escaping & Dereferencing
| Quoting, Escaping & Dereferencing | |
|
Symbol |
Meaning |
| ` | Back quotes. Text within backquotes is treated as a command.
Usage : `ls -l` `ls -l | wc` They are also used as substituting the command. |
| $ | A dollar sign is used to expand or dereference the variable. For example
echo $SHELL /bin/bash |
| ‘ | The single quote ( ‘quote’ ) protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters.
VARIABLE – NO WILDCARDS – NO COMMAND SUBSTITUTION – NO echo ‘$SHELL’ $SHELL |
| Quoting, Escaping & Dereferencing | |
|
Symbol |
Meaning |
| \ | A backslash is used to escape the special meaning of the characters. You can use \ before dollar sign to tell the shell to have no special meaning.
echo “My Shell is \$SHELL” My Shell is $SHELL |
| “ | The double quote ( “quote” ) protects everything enclosed between two double quote marks except dollar sign, backquote, double quote & backslash.
VARIABLE – YES WILDCARDS – NO COMMAND SUBSTITUTION – YES echo “$SHELL” /bin/bash echo “ My working directory is `pwd` ” /home/cse echo “\$SHELL” $SHELL
|
| Examples: Single Quote |
echo '$USER is $USER' $USER is $USER
|
echo 'today is `date`' today is `date`
|
echo 'No background&' No background&
|
echo 'I said, "radio!"' I said, "radio"
|
| REASON: Inhibits all substitution, and the special meaning of metacharacters. |
| Examples: Double Quote, Escaping & Dereferencing |
echo "My name is $USER " My name is pythonbaba
|
echo "\$2.00 says `date`" $2.00 says Sun Jan 8 01:43:32 EST 200
|
echo “I am using bash \$SHELL " I am using bash $SHELL
|
echo “ My “name is $USER and “ I am using $SHELL “ My names is pythonbaba and I am using /bin/bash
|
REASON:
|
Command Substitution, Sequences ,Grouping, & Conditional Commands
| Command Substitution |
| What is the use of command substitution? |
| • Sometimes we want to use the output of one command as part of another command.
• This may be because it is just easier to run a command than to type the output produced from the command. |
| There are two valid forms for expressing command substitution.
The original Bourne(can also be used with BASH shell): `command` The Bash’s own extension: $(command) |
ls -l `pwd`
-rwxr-xr-x 1 root root 139456 Jan 8 20:14 /usr/bin/less
|
ls -l $(pwd)
-rwxr-xr-x 1 root root 139456 Jan 8 20:14 /usr/bin/less
|
| Sequences: Sequential execution of commands |
| Sometimes we need to combine several commands.
This facility is useful for type-ahead (and think-ahead) addicts who like to specify an entire sequence of actions at once. A sequence of commands can be entered on one line. Each command must be separated from its predecessor by semicolon. There is no direct relationship between the commands. command1 ; command 2 ; command3 |
| Example |
echo $SHELL ; pwd; ls
/bin/bash /home/pythonbaba a.c b.c cc.c dir1 dir2
|
| Example |
| Each command in a sequence may be individually I/O redirected as well: |
echo $SHELL > shell.txt ; ls ; pwd > pwd.txt
a.c b.c cc.c date.txt dir1 dir2
|
cat shell.txt
/bin/bash
|
cat pwd.txt
/home/pythonbaba
|
| Grouping: Grouping of Commands |
| If we apply the same operation to the group (what we have applied in sequencing), we can group commands.
Commands are grouped by placing them into parentheses, which causes them to be executed by a child shell (subshell) Note: we will refer to subshell in the coming slides. |
| How to perform grouping of commands |
| ( command 1 ; command2; ….. ; ….; command n)
We can also redirect the output to a file ( command 1 ; command2; ….. ; ….; command n) > anyfile |
| Examples |
( echo $SHELL ; ls ; pwd )
/bin/bash a.c b.c cc.c date.txt dir1 dir2 /home/pythonbaba
|
| Example |
( echo $SHELL ; ls ; pwd ) > file.txt cat file.txt
/bin/bash a.c b.c cc.c date.txt dir1 dir2 /home/pythonbaba
|
| Conditional: Conditional Execution of commands |
| • We can combine two or more commands using conditional relationships :
AND (&&) OR (||). • If we AND two commands, the second is executed only if the first is successful. • If we OR two commands, the second is executed only if the first fails. |
| • Basic Syntax |
| command 1 && command2 |
| command 1 || command2 |
| Example |
cp file1 file2 && echo “Copy successful”
|
| If the content of file 1 is copied to file 2, then echo command would be executed |
cp file1 file2 || echo “Copy failed”
|
| • If the content of file1 is copied to file2 then echo command will not execute.
• If the cp commands fail then echo command will be executed. |
Background Process
| Background Processing: Using & operator |
| • If you follow a simple command, pipeline, a sequence of pipelines, or a group of commands by the “&” metacharacter, a subshell is created to execute the commands as a background process.
• The background process runs concurrently with the parent shell and does not take control of the keyboard. • It means you immediately get a new prompt and can continue your work while the command is running. • The background process runs in a subshell, created by the parent shell in which you typed the command. • Background processing is therefore very useful for performing several tasks simultaneously, as long as the background tasks do not require input from the keyboard. |
| Using Background operator & |
| • Suppose you want to copy all files and subdirectories from /usr directory to /tmp
• Since /usr directory contains a lot of files and subdirectories, it will take some time to copy it to /tmp directory |
| There are two ways to do it |
Either you can wait for the next command to be typed, till you are done with copying.
cp /usr /tmp
|
Or you can immediately get the command prompt to type another command, while copying is done as a background process
cp /usr /tmp&
Concatenate & operator at the end of your command. |
| REDIRECTION OF BACKGROUND PROCESSES: Redirecting Output |
| To prevent the output from a background process from arriving at your terminal, redirect its output to a file. |
cp /usr /tmp > out.txt&
|
