11.7. Metacharacters
Metacharacters are special characters used to represent something other than themselves. Table 11.6 lists some common Korn shell metacharacters and their functions.
Table 11.6. Korn Shell MetacharactersCommand | Function |
---|
\ | Literal interpretation of the following character | & | Background processing | ; | Command separator | $ | Variable substitution | ? | Match for a single character | [abc] | Match for one from a set of characters | [!abc] | Match for one not from the set of characters | * | Match for zero or more characters | (cmds ) | Execute commands in a subshell | {cmds} | Execute commands in current shell |
Example 11.26.
1 $ ls -d * all files are displayed
abc abc122 abc2 file1.bak file2.bak nonsense nothing one
abc1 abc123 file1 file2 none noone
2 $ print hello \ # The carriage return is escaped
> there
hello there
3 $ rusers& # Process the rusers command in the background
[1] 4334
$
4 $ who; date; uptime # Commands are executed one at a time
ellie console Feb 10 10:46
ellie ttyp0 Feb 15 12:41
ellie ttyp1 Feb 10 10:47
ellie ttyp2 Feb 5 10:53
Mon Feb 15 17:16:43 PST 2004
5:16pm up 5 days, 6:32, 1 user, load average: 0.28, 0.23, 0.01
5 $ print $HOME # The value of the HOME variable is printed
/home/jody/ellie
6 $ print $LOGNAME # The value of the LOGNAME variable is printed
ellie
7 $ ( pwd; cd / ; pwd )
/home/jody/ellie
/
$ pwd
/home/jody/ellie
8 $ { pwd; cd /; pwd; }
/home/jody/ellie
/
$ pwd
/
9 $ ( date; pwd; ls ) > outfile
$ cat outfile
Mon Feb 15 15:56:34 PDT 2004
/home/jody/ellie
foo1
foo2
foo3
EXPLANATION
The asterisk matches all the files in the current directory. (The –d option to the ls command prevents the contents of subdirectories from being displayed.) The backslash escapes the newline so that the command line can be continued on the next line. The ampersand (&) causes the rusers program to be executed in the background; the shell prompt returns immediately. Both processes will run concurrently. Each command is separated by a semicolon. Each command will be executed one at a time. If a dollar sign precedes the variable, the shell will perform variable substitution. The value of the env variable, HOME, is displayed. The HOME variable contains the full pathname of the user's home directory. Again, the dollar sign precedes the variable name. The value of the LOGNAME variable is the user's login name. The parentheses indicate that the enclosed commands will be executed in a subshell. The cd command is built into the shell, so that each shell that is invoked has its own copy of cd. The pwd command prints the present working directory, /home/jody/ellie. After cding to the root directory, the pwd command displays that the new directory is root. After the subshell exits, the output of the pwd command in the parent shell indicates that the directory is still set to /home/jody/ellie as it was before entering the subshell. The curly braces indicate that the enclosed commands will be executed in the current shell. The pwd command prints the present working directory, /home/jody/ellie. After cding to the root directory, the pwd command displays that the new directory is root. After the commands within the curly braces exit, the output of the pwd command indicates that the directory is still set to the root directory. The parentheses are used to group the commands so that the output of all three commands is sent to outfile.
|