Reserved Characters

The various flavors of shell use certain characters (referred to as metacharacters) which have special (or reserved) meanings. Additional characters have special meanings when used in what linux calls regular expressions. And just to make things interesting, some characters can be used in both these situations and may have different meanings.

The following table lists some of the most common metacharacters

Metacharacters Explanation
*

wildcard for anything; there are no limits on things like the number of characters or what characters (aside from what are called special characters and the metacharacters described here) can be inserted in place of the *; for example, you can show all of the DigitalMicrograph data files in the current area using

$ ls *.dm3
?

wildcard for a single character; there are no limits on what characters (aside from special characters and metacharacters) can be inserted in place of the ?; for example, you can see the first images in a recorded numeric sequence using

$ ls -l myImages_0000?.dm3
[ ]

delimiter for a range of single character wild cards; for example, if you only wanted to list images from a numeric series where the files of interest were in the range 140 thru 179, you could use this command

 $ ls -l myImages_001[4567]?.dm3
or $ ls -l myImages_001[4-7]?.dm3

the [ ] delimiters say "replace this one character with 4 or 5 or 6 or 7 and nothing else" (and as shown in the second example above, a continuous range can be specified using the beginning and end of the range with a dash ("-") between them)

you can also look for specific letters or alphabetic regions of letters using [ ]'s; the command

$ ls -l [A-Z]*

will show all files that start with capital letters while

$ ls -l [A-z]*

will show all files that start with a letter (opposed to a number or another character, and bear in mind that [a-Z] does not work at all!); finally,

$ ls -l [ACFx]*[02468]

will show all files that start with A, C, F or x and end with an even number

 

NOTE: an extremely useful thing to do in order to understand linux in general and the use of these delimiters and the wildcard symbols is to read about what linux refers to as regular expressions. For example, most linux books have chapters covering what they will call "pattern matching" or "regular expressions." Some especially useful resources are the O'Reilly Media books Sed & Awk and Mastering Regular Expressions (and the O'Reilly pocket guides to both).

 

|

the | symbol (called a "pipe") is used for pipelining commands, meaning that the output of one command becomes the input to the next command

$ cmd1 | cmd2 | cmd3 | cmd4 etc.
;

the semi colon (;) is a "command separator" and it allows you to string a series of commands onto a single command line; this is quite different from the pipe in that the commands strung together do not depend on each other for inputs

as an example, if you are actively working in one area of the computer, but want to move someplace else and look at how much has been accomplished in that area, you could put together a series of commands that would do this and return you to the starting location:

$ cd $HOME/ProjectX ; ls -ltr ; cd -

since this command will be stored in your terminal's "history," it would be easy to invoke this "single line command" whenever you wanted to examine what had occurred in the ProjectX area (i.e., recalling this single line is easier than recalling or re-typing the three individual commands)

&

the ampersand (&) causes a command to run in the shell's "background" (meaning that the user continues to have access to the interactive shell); for example, if you start a graphical text editor from the command line without using the &, the editor will run but your terminal will be inactive until you exit the editor; instead, the editor should be started and run in the background using this syntax:

$ nedit myTextFile.txt &
>, >> and <

these symbols are called redirection operators and they allow the user to manipulate where stdin, stdout and stderr (names for the linux standard input/output channels) end up; for example, the stdout output from a command can be directed into a new file using

$ cmd [options] [target] > newFile

or into an existing file using

$ cmd [options] [target] >> oldFile

in this second example, it does not matter if oldFile exists or not: the ">>" redirection command will create the file if necessary but append to an existing file; if the ">" redirection command is used, the file will always be new and an existing file with the same name will be over-written (and lost)

NOTE: each shell has a different syntax for simultaneously redirecting stdout and stderr into different places or combining stdout and stderr into a single stream that goes to one place

the "<" operator can be used when a command expects or can use the contents of a file as input:

$ cmd < existingFile
" "

a pair of double quotes (the " symbol) encloses an expression where you want to see everything exactly as it is written except for any parts that involve a $, a back quote (`) or a backslash (\); the easiest way to see the difference between double and single quotes (the next entry in table) is to use the echo command:

$ echo "My home is ${HOME}"

produces this line

My home is /N/u/myUserName/Quarry
' '

a pair of single quotes (the ' symbol) encloses an expression where you want to see everything exactly the way it is written; in this case, the command

$ echo 'My home is ${HOME}'

produces

My home is ${HOME}

the key here is that when enclosed within the single quotes, ${HOME} is not replaced by ("promoted to") the current value of the HOME variable

`

back quotes (the ` symbol) cause something called command substitution, which means that a command placed between a pair of back quotes is executed and the output of the command replaces everything from the first ` to the second `; back quotes are protected by single quotes (command substition does not occur) but not by double quotes; we can easily demonstrate this with the echo command

$ echo "Today is ` date `"

produces this line

Today is Thu May 16 19:47:27 EDT 2013

while

$ echo 'Today is ` date `'

produces this line

Today is ` date `
\

the \ (backslash) has a number of uses, only some of which will be described here; one major use is when quoting the $, ` and " characters; in this context, the character which follows the \ is taken literally; this means, for example, instead of the back quote (`) causing command substitution, the \` is simply the back quote character (`); a "tech savvy" way to say this is that the ` "escapes" its normal function due to the backslash, and constructs using the \ are referred to as "backslash escapes"

a simple example of this can be shown using the echo command again

$ echo "Today is ` date `"

produces

Today is Thu May 16 19:47:27 EDT 2013

while

$ echo "Today is \` date \`"

produces

Today is ` date `

this is a rather trivial example that could have been produced using single quotes and no backslashes, but there will be circumstances where you want to output or use a $, ` or "; in those cases, you can substitute \$, \` and \" for the single character you really want

for example, if you wanted to output a line that says

A line with command substitution contains a phrase like `date`

and simply echoed the phrase using

$ echo "A line with command substitution contains a phrase like `date`"

or

$ echo A line with command substitution contains a phrase like `date`

the output would be

A line with command substitution contains a phrase like Thu May 16 19:47:27 EDT 2013

instead, you need to tell the echo command that you want the ` to really be a `

$ echo "A line with command substitution contains a phrase like \`date\`"

another use of the \ is for formating output, where character pairs such as \t have special meanings (the "tab" symbol, in this case); for example, if you want to format something using tabs (\t) and new lines (\n), you could use the following single line echo command

          $ echo -e "Output\n\n\tGroup\tUsers 
              \tTime\n\n\t1\t6\t1:45\n\t2 
              \t3\t6:30\n\nTotal:\t9\t8:15\n"
to produce this sort of output
        Output:
        
                 Group    Users    Time
                 1        6        1:45
                 2        3        6:30
        
        Total:            9        8:15
          

note that the -e option to echo tells it to expect (and interpret properly) the character pairs starting with a \ (another set of "backslash escapes"); remember that for these backslash escapes to work, the entire line needs to be enclosed within double or single quotes

!

the ! (an exclamation mark but often referred to as a bang) has several special uses

when used as a logical operator, the ! means "not"; for example, the command

$ ls -l myImages_001[4-7]?.dm3

was described previously and means "show the images whose numbers are in the region 140 through 179"; if you use this command instead

$ ls -l myImages_001[!4-7]?.dm3

the output is the files named myImages_001??.dm3 except those numbered 140 through 179 (i.e., not 140-179); note that the 1 followed by 2 ?'s (single character wild card characters) here represents the numbers 100 through 199; this use of ! can also occur in shell scripts where logical flow through the script is needed (i.e., do something if a condition is not true)

most flavors of shell have an extensive "memory" of the the most recent commands (referred to as the "history" of that instance of the shell), and ! can also be used as the beginning of what is called "history substitution"; for example, if you wanted to re-run the last command you had typed that started with the letters "ec" (for example, echo), you can simply type

$ !ec

this executes exactly whatever the last command was that began with "ec"; there are far too many ways to use the ! symbol for history substitution to cover here, but you may find it useful to consult a linux text to learn some of them

finally, #! (pronounced "shebang" or "hash-bang") is a special symbol pair that is used as the first line of a shell script to invoke a particular shell; for example, if you want a script to run the Z shell (and there really is a Z shell!), start the script using

#!/bin/zsh
#

the # symbol is often called a "hash" or "pound" and the use of hash-bang (#!) was described above; the other most common use of # is to signify a comment in programming and scripting languages; it essentially means "ignore this symbol and the rest of this line"

/

the / (forward slash) symbol is used to delineate and separate directory names when describing a location (e.g., cd $HOME/ProjectX/Images/2013_01_30 means move to a place called "2013_01_30" which is located in an area called "ProjectX" which in turn is located in your home directory)

when used alone (e.g., cd /), the / symbol indicates "the root of the filesystem" (i.e., the place where the full pathname to all files and directories starts)

~

the most common use and meaning for the ~ symbol (the "tilde") is simply "your home directory" and (for example) cd ~ is one of many ways to move from anywhere to your home

you will also sometimes see the ~ appended to the end of file name where the file has been edited using the emacs text editor: the appended ~ signifies that a file is the backup copy of an edited file

another use for the ~ is in conjunction with a user's name on the computer system (e.g., ~userName); in this case, ~userName indicates the home directory for a user called userName; since the IU computer clusters will not allow you to look at another user's home area, this is not so useful on those machines

 

NOTE: this list of metacharacters ignores the use of these same symbols in mathematical expressions, when used with commands like sed, grep and awk and when running text editors like ex, ed, and vi

 

Other metacharacters exist, and more detailed descriptions of how they operate can be found on-line and in linux reference books.