Previous Table of Contents Next


HISTORY SUBSTITUTION

History substitution allows previous commands to be modified and re-executed. The history variable specifies the number of previous commands accessible in the current shell. The savehist variable specifies the number of commands to save in the ~/.history file between login sessions.

Format: event:word:modifiers

event previous command from history file:
!n command n
!n nth last command
!string command that begins with string
!?string command that contains string
!! previous command
!{...} separate history substitution from adjacent characters
^x^y substitute x for y on previous command line
word individual word from previous command (: can be omitted if word begins with ^, $, *, –, or %).
# entire command line
0 first word
n nth argument
^ first argument
$ last argument
% word matched by last ?string search
xy range of words from x to y
y range of words from first to y
* all arguments (no match if only one word)
x range of words x to second to last argument

modifier controls command/word selection (multiple modifiers must be separated with a : character). If prefixed with g, then the modification is applied to all arguments.
e return filename suffix of first argument
h return root pathname part of first argument
p display new command, but do not execute
q quote substituted words to prevent further substitution
r return filename prefix of first argument
s/x/y substitute first occurrence of x for y
t return tail pathname part of first argument
x same as q, except quote words individually
& repeat previous substitution

FILENAME SUBSTITUTION

Unquoted words containing any of the following characters are expanded to a sorted list of filenames from the current directory. The “.” and “/” characters must be explicitly matched.

Pattern-Matching Characters

? match any single character
* match zero or more characters
[abc] match any characters between the brackets
[x–z] match any character or characters in the range x to z
[a–ce–g] match any characters in the range a to c, e to g
{s1, s2, ...} match any string in the given set
~ substitute the home directory of the invoker
~user substitute the home directory of user

JOB CONTROL

Job control is a process manipulation feature that allows programs to be stopped and restarted, moved between the foreground and background, their processing status to be displayed, and more. When a program is run in the background, a job number and process id are returned. Jobs being executed in the background will stop and notify you if they attempt to read input from the terminal. If notify is set, the Bourne shell will immediately notify you of blocked jobs.

Job Control Commands

bg [%n] put current or stopped job n in the background
fg [%n] move current or background job n into foreground
jobs display the status of all jobs
jobs –l display status of all jobs along with their process ids
kill [signal] [%n | PID] send signal to job n, current job, or specified process-ID (default TERM)
kill –l list all valid signal names
notify [%n] notify user when status of current job or job n changes
stop [%n] stop current or specified job n
stty []tostop allow/prevent background jobs from generating output
suspend suspend execution of the current shell
wait wait for all background jobs to complete
Ctl-z stop the current job

Job Name Format

%, %%, %+ current job
%n job n
%– previous job
%string job whose name begins with string
%?string job that matches part or all of string

QUOTING

Quotes are used when assigning values containing whitespace or special characters, to delimit variables, and to assign command output.

'...' remove the special meaning of enclosed characters except '
"..." remove the special meaning of enclosed characters except $, ', !, and \
\c remove the special meaning of character c
`command replace with the standard output of command

VARIABLES

Like in other high-level programming languages, variables are used by the C shell to store values. Variable names can begin with an alphabetic or underscore character, followed by one or more alphanumeric or underscore characters. Variable names that contain only digits or special characters are reserved for special variables set directly by the C shell.

Variable Assignment Format

set variable set variable to null
set variable=value set variable to value
set variable=(value1 value2 . . . valuen) set variable to value1 through valuen
set variable[n]=value set variable element n to value
set variable[n]=(value1 value2 . . . valuen) set variable element n to value1 through valuen
unset variable remove the definition of variable
@ variable=expression set variable to numerical value of expression
@ variable[n]=expression set variable element n to numerical value of expression


Previous Table of Contents Next
1