Substitutions

The shell lets you use a shorthand notation to include the values of certain things in the command line.

The shell does the following substitutions, in this order:

  1. directories—tilde expansion
  2. parameters
  3. commands
  4. arithmetical expressions
  5. braces
  6. filename generation

Let's look at these in more detail:

Directories—tilde expansion
The shell interprets the tilde character (~) as a reference to a user's home directory. The characters between the tilde and the next slash (if any) are interpreted as the name of a user.

For example, ~mary/some_file refers to some_file in the home directory of the user named mary. If you don't specify a user name, it's assumed to be yours, so ~/some_file refers to some_file in your home directory.

Note: Your home directory is defined in your entry in the password database; see the description of /etc/passwd in Managing User Accounts.
Parameters
To include the value of a parameter on the command line, put a dollar sign ($) before the parameter's name. For example, to display the value of your PATH environment variable, type:
echo $PATH
  
Commands
Sometimes, you might want to execute a command and use the results of the command in another command. You can do it like this:
$(command)
  

or with the older form, using backquotes:

`command`
  

For example, to search all of your C files for a given string, type:

grep string $(find . -name "*.c")
  

The find command searches the given directory (. in this case) and any directories under it for files whose names end in .c. The command substitution causes grep to search for the given string in the files that find produces.

Arithmetical expressions
To specify an arithmetical expression in a command line, specify it as follows:
$(( expression ))
  

For example:

echo $((5 * 7))
  
Note: You're restricted to integer arithmetic.
Braces
You can use braces to add a prefix, a suffix, or both to a set of strings. Do this by specifying:
[prefix]{str1,…,strN}[suffix]
  

where commas (,) separate the strings. For example, my_file.{c,o} expands to my_file.c my_file.o.

Filename generation
Instead of using a command to work on just one file or directory, you can use wildcard characters to operate on many.
If you want to: Use this wildcard:
Match zero or more characters *
Match any single character ?
Match any characters (or range of characters separated by a hyphen) specified within the brackets []
Exclude characters specified within brackets !
Note: Hidden files, whose names start with a dot (e.g., .profile), aren't matched unless you specify the dot. For example, * doesn't match .profile, but .* does.

The following examples show you how you can use wildcards with the cp utility to copy groups of files to a directory named /tmp:

If you enter: The cp utility copies:
cp f* /tmp All files starting with f (e.g., frd.c, flnt)
cp fred? /tmp All files beginning with fred and ending with one other character (e.g., freda, fred3)
cp fred[123] /tmp All files beginning with fred and ending with 1, 2, or 3 (i.e., fred1, fred2, and fred3)
cp *.[ch] /tmp All files ending with .c or .h (e.g., frd.c, barn.h)
cp *.[!o] /tmp All files that don't end with .o
cp *.{html,tex} All files that end with .html or .tex