Mar
27
2012

Unix Shell Metacharacters

Metacharacters are those characters which hold special significance. There are numerous metacharacters in Linux. We will try to discuss the significance of each one of them through simple unix commands.

Different metacharacters in Unix can be grouped in the following way :

1. Filename Substitution Metacharacters

*   -   This wild card character represents any combination of number or characters.

?    -   This metacharacter stands for one character.

[....]   -   This gives the shell an option from the specified range.

[!...]   -   This is just the opposite metacharacter to the one discussed previously. It considers all those choices other than the ones specified. ( ! standing for NOT )

Usage Examples

Consider a directory named my_project which has the files bbc.c , function.c , function1.c , goal.c , main.c , bbc.h , function.h , function1.h and goal.h

All the commands in this category would be run considering my_project as the current diretory.

ls f*   -  Lists all the file names starting with f in the current directory i.e function.c , function.h , function1.c and function1.h

ls f*.c   -   List the files function1.c and function.c i.e the files in the directory my_project beginning with f and having an extension .c

ls [a-e]*    -   Lists all the files starting with either a,b,c,d and e i.e bbc.c and bbc.h. (Note : There is no space between the opening brackets and a or closing brackets and e.)

ls ????.c    -   Lists all those file names which are of 4 characters and are C files. Thus it lists goal.c and main.c

ls ?b?   -   Lists all those files which are 3 characters long and whose second character is b. As we don’t have any files with such 3 characters without an extension we get ls: cannot access ?b?: No such file or directory

ls [a-e]?[a-e].c   -   Lists all the C files whose first character if either a,b,c,d or e and the third character is also in the range a to e. Hence it would list just bbc.c

Note : The files which we want to list should be of 3 characters , neither more nor less. Thus a file like abcd.c is not listed when the above command is executed because the files we listed had to be of 3 characters.

ls [!a-e]?[!a-e]   -   Lists all those files which are 3 characters long and which don’t start and end with characters from a through e. Since we don’t have such files we get the message ls: cannot access [!a-e]?[!a-e]: No such file or directory


2. I/O Redirection and Piping

<   -   Used to specify the standard input.

>    -  Used to add contents to a file.

>>    -   Used to append to a file. If the ‘ >>’  symbol is not used and content is added to a file using only the ‘ >’ symbol the previous content of the file is deleted and replaced with the new content.

|   -   Used to make the standard output of one command the standard input of another command.

Usage Examples

cat file1 > file2    -   Adds the content of the file file1 into a new file file2. If the ‘ >> ‘ is used the contents of file1 are appended to file2.

cat < file1 > file2   -   the input of the cat command is file1 and the output of the the cat command is written to file.

Note: Both the above commands carry out the same task.

ls -l | wc    -   Prints newline, word, and byte count for the command ls -l ( i.e long list of the files )

ls -l | wc > results   -   The newline , word and byte count of the long list of files is stored in a file called as results in the same directory.


3. Process Execution

;    -   The semicolon is used to run multiple commands all at once one after the other.

()    -   Parenetheses are used to execute the commands in a sub-shell.

&    -   The ampersand symbol is used to execute a process in the background( used specially when we want something which is really time-consuming to occur in the background while we can still work in the foreground).

&&    -   The command followed by the && is performed only if the first only is successful.

||    -   The command followed by || is performed only if the first one fails.

Usage Examples

ls; date    -   Lists all the files in the current directory and then prints the date

( cd my_project , pwd )    -   The parentheses cause a sub-shell to be invoked in which a change in directory to my_project is done and the working directory printed. The sub-shell now ceases to exist and in the current shell we are still in our old directory.

mv /home/*.* /etc/ &    -   Used to move all the contents of home directory to the etc folder in the root( / ) directory where & carries out the work in the background while we can do other things as we want in the foreground.

find . -name project && date    -   Prints the system date only if the first command is executed successfully ( in this case if a file known as project is found in our current directory ).

find . -name project || date    -   prints the system date only if the file called project is not found in the current directory.


4. Quoting metacharacters

\    -   This forward slash is used to take away the significance hold by any of the metacharacters.

‘ ‘    -   The single quotes are used to take every enclosed character literally.

` `    -   The back quotes or the accent graves are used to replace the command they enclose with its output.

” ”    -   Double quotes allow some metacharacters ( namely $ , \ and back quotes ` ` ) to hold their special status.

Usage Examples

expr 3 * 4    -   Results in an error as the shell interprets * as a wild character.

expr 3 \* 4    -   Prints the result obtained by multiplying 3 by 4 which is 12

expr 3 ‘*’ 4    -   The single quotes take the sign * literally ( i.e as a muliplicative sign ) and output 12.

echo `expr 3 ‘*’ 4`    -   Here the output of the evaluated expression is passed to the echo command using the back quotes and as a result 12 is printed at the prompt.

echo ‘expr 3 ‘*’ 4′   – With the single quote the output is expr 3 * 4

echo `expr 3 * 4`    -   Without the single quotes in front of the multiplication sign gives us a syntax error.

If we have a variable, name=”fortystones” then
echo “$name”    -  Outputs fortystones
echo ‘$name’    -   With single quotes simply prints $name.


5. Positional parameters

Positional parameters are those variables defined by the shell which are nine in number named $1 through $9.

If an executable file called SS is executed as follows:
./SS Welcome to Fortystones
Here the shell assigns $0 to ./SS , $1 to Welcome and so on. If there are more than 9 such characters the shift command has to be used.

Positional arguments greater than 9 use braces to refer to them like ${10}, ${11}…

The shift command discard $1 and renumbers all the other variables. “shift N” will shift N arguments at once.


6. Special characters

The special characters designated by the shell are :
$#    -   Total number of positional parameters

$0   -   Name of the command being executed

$-    -   Current shell settings

$$    -   PID of current shell

$?    -   Exit status of the last executed command

$*    -   List of all shell arguments in a single string, with one space separating them.

$@   -    Similar to $*

Usage Examples

echo $-    -   Displays the current shell setting

echo $?    -   Reports the exit status of the command. The value 0 is printed if the command was executed and other values if it was cancelled or postponed( using Ctrl + Z or Ctrl + C )

echo $$    -   Displays the PID of the current shell

If the argument list is:
file1 file2 file3
echo $#    -   As the total number of arguments are 3 it has the value 3

echo $*    -   file1 file2 file3

echo $@    -   file1 file2 file3

Share

Related Posts

About the Author: Raju Khanal

An IIIT student, pursing his B.Tech degree in the IT, is one of the other aspiring authors of fortystones. He is a passionate learner and a wanna-be coder. He loves Linux and always plays with it in his spare time.

3 Comments + Add Comment

  • I am new to linux. This is best , clearly explained, easy for new and non technicals learners like me.

    Thanks a lot I am happy to find this article.

    • Thank You for the encouraging words. Check out our Linux section for more articles. Cheers!

  • I study Linux at school,
    From book, Internet pages to extra sources of information there is nowhere an explanation like this one.
    Like Sunny says: This is the best!
    Like I say: This is the best!
    Like anybody would say:This is the best!
    You can be proud of this article.
    And I bookmarked this page.

    # Mayby you should give some general Information on what group, what is and where is used for.
    but is is also fine the way it is now.
    Really, many thanks!

Leave a comment

*