csh - shell command interpreter with a C-like syntax
csh [-bcefinstvVxX] [argument]...
csh, the C shell, is a command interpreter with a syntax reminiscent of the C language. It provides a number of convenient features for interactive use that are not available with the Bourne shell, including filename completion, command aliasing, history substitution, job control, and a number of built-in commands. As with the Bourne shell, the C shell provides variable, command and filename substitution.
When first started, the C shell normally performs commands from the .cshrc file in your home directory, provided that it is readable and you either own it or your real group ID matches its group ID. If the shell is invoked with a name that starts with `-', as when started by login(1), the shell runs as a login shell.
If the shell is a login shell, this is the sequence of invocations: First, commands in /etc/.login are executed. Next, commands from the .cshrc file your home directory are executed. Then the shell executes commands from the .login file in your home directory; the same permission checks as those for .cshrc are applied to this file. Typically, the .login file contains commands to specify the terminal type and environment. (For an explanation of file interpreters, see Command Execution and exec(2).)
As a login shell terminates, it performs commands from the .logout file in your home directory; the same permission checks as those for .cshrc are applied to this file.
After startup processing is complete, an interactive C shell begins reading commands from the terminal, prompting with hostname% (or hostname# for the privileged user). The shell then repeatedly performs the following actions: a line of command input is read and broken into words. This sequence of words is placed on the history list and then parsed, as described under USAGE. Finally, the shell executes each command in the current line.
When running noninteractively, the shell does not prompt for input from the terminal. A noninteractive C shell can execute a command supplied as an argument on its command line, or interpret commands from a file, also known as a script.
The following options are supported:
-b
-c
-e
-f
-i
-n
-s
-t
-v
-V
-x
-X
Except with the options -c, -i, -s, or -t, the first nonoption argument is taken to be the name of a command or script. It is passed as argument zero, and subsequent arguments are added to the argument list for that command or script.
When enabled by setting the variable filec, an interactive C shell can complete a partially typed filename or user name. When an unambiguous partial filename is followed by an ESC character on the terminal input line, the shell fills in the remaining characters of a matching filename from the working directory.
If a partial filename is followed by the EOF character (usually typed as Control-d), the shell lists all filenames that match. It then prompts once again, supplying the incomplete command line typed in so far.
When the last (partial) word begins with a tilde (~), the shell attempts completion with a user name, rather than a file in the working directory.
The terminal bell signals errors or multiple matches. This bell signal can be inhibited by setting the variable nobeep. You can exclude files with certain suffixes by listing those suffixes in the variable fignore. If, however, the only possible completion includes a suffix in the list, it is not ignored. fignore does not affect the listing of filenames by the EOF character.
The shell splits input lines into words at space and tab characters, except as noted below. The characters &, |, ;, <, >, (, and ) form separate words; if paired, the pairs form single words. These shell metacharacters can be made part of other words, and their special meaning can be suppressed by preceding them with a `\' (backslash). A newline preceded by a \ is equivalent to a space character.
In addition, a string enclosed in matched pairs of single-quotes ('), double-quotes ("), or backquotes (`), forms a partial word. Metacharacters in such a string, including any space or tab characters, do not form separate words. Within pairs of backquote (`) or double-quote (") characters, a newline preceded by a `\' (backslash) gives a true newline character. Additional functions of each type of quote are described, below, under Variable Substitution, Command Substitution, and Filename Substitution.
When the shell's input is not a terminal, the character # introduces a comment that continues to the end of the input line. Its special meaning is suppressed when preceded by a \ or enclosed in matching quotes.
A simple command is composed of a sequence of words. The first word (that is not part of an I/O redirection) specifies the command to be executed. A simple command, or a set of simple commands separated by | or |& characters, forms a pipeline. With |, the standard output of the preceding command is redirected to the standard input of the command that follows. With |&, both the standard error and the standard output are redirected through the pipeline.
Pipelines can be separated by semicolons (;), in which case they are executed sequentially. Pipelines that are separated by && or || form conditional sequences in which the execution of pipelines on the right depends upon the success or failure, respectively, of the pipeline on the left.
A pipeline or sequence can be enclosed within parentheses `()' to form a simple command that can be a component in a pipeline or sequence.
A sequence of pipelines can be executed asynchronously or "in the background" by appending an `&'; rather than waiting for the sequence to finish before issuing a prompt, the shell displays the job number (see Job Control, below) and associated process IDs and prompts immediately.
History substitution allows you to use words from previous command lines in the command line you are typing. This simplifies spelling corrections and the repetition of complicated commands or arguments. Command lines are saved in the history list, the size of which is controlled by the history variable. The most recent command is retained in any case. A history substitution begins with a ! (although you can change this with the histchars variable) and occurs anywhere on the command line; history substitutions do not nest. The ! can be escaped with \ to suppress its special meaning.
Input lines containing history substitutions are echoed on the terminal after being expanded, but before any other substitutions take place or the command gets executed.
An event designator is a reference to a command line entry in the history list.
!
!!
!n
!-n
!str
!?str?
!?str? additional
!{command} additional
^previous_word^replacement^
!:s/previous_word/replacement/.
To re-execute a specific previous command AND make such a substitution, say, re-executing command #6,
!:6s/previous_word/replacement/.
A `:' (colon) separates the event specification from the word designator. It can be omitted if the word designator begins with a ^, $, *, - or %. If the word is to be selected from the previous command, the second ! character can be omitted from the event specification. For instance, !!:1 and !:1 both refer to the first word of the previous command, while !!$ and !$ both refer to the last word in the previous command. Word designators include:
#
0
n
^
$
%
x-y
*
x*
x-
After the optional word designator, you can add one of the following modifiers, preceded by a :.
h
r
e
s/l/r/
t
&
g
p
q
x
Unless preceded by a g, the modification is applied only to the first string that matches l; an error results if no string matches.
The left-hand side of substitutions are not regular expressions, but character strings. Any character can be used as the delimiter in place of /. A backslash quotes the delimiter character. The character &, in the right hand side, is replaced by the text from the left-hand-side. The & can be quoted with a backslash. A null l uses the previous string either from a l or from a contextual scan string s from !?s. You can omit the rightmost delimiter if a newline immediately follows r; the rightmost ? in a context scan can similarly be omitted.
Without an event specification, a history reference refers either to the previous command, or to a previous history reference on the command line (if any).
^l^r^
!:s/l/r/.
The C shell maintains a list of aliases that you can create, display, and modify using the alias and unalias commands. The shell checks the first word in each command to see if it matches the name of an existing alias. If it does, the command is reprocessed with the alias definition replacing its name; the history substitution mechanism is made available as though that command were the previous input line. This allows history substitutions, escaped with a backslash in the definition, to be replaced with actual command line arguments when the alias is used. If no history substitution is called for, the arguments remain unchanged.
Aliases can be nested. That is, an alias definition can contain the name of another alias. Nested aliases are expanded before any history substitutions is applied. This is useful in pipelines such as
alias lm 'ls -l \!* | more'
which when called, pipes the output of ls(1) through more(1).
Except for the first word, the name of the alias can not appear in its definition, nor in any alias referred to by its definition. Such loops are detected, and cause an error message.
The following metacharacters indicate that the subsequent word is the name of a file to which the command's standard input, standard output, or standard error is redirected; this word is variable, command, and filename expanded separately from the rest of the command.
<
<<word
> >! >& >&!
When set, the variable noclobber prevents destruction of existing files. It also prevents redirection to terminals and /dev/null, unless one of the ! forms is used. The & forms redirect both standard output and the standard error (diagnostic output) to the file.
>> >>& >>! >>&!
The C shell maintains a set of variables, each of which is composed of a name and a value. A variable name consists of up to 128 letters and digits, and starts with a letter. An underscore (_) is considered a letter). A variable's value is a space-separated list of zero or more words. If the shell supports a variable name upto 128 characters the variable SUNW_VARLEN is defined. If a variable name of up to 128 characters is not supported, then an older version of the shell is being used, and the shell variable name length has a maximum length of 20.
To refer to a variable's value, precede its name with a `$'. Certain references (described below) can be used to select specific words from the value, or to display other information about the variable. Braces can be used to insulate the reference from other characters in an input-line word.
Variable substitution takes place after the input line is analyzed, aliases are resolved, and I/O redirections are applied. Exceptions to this are variable references in I/O redirections (substituted at the time the redirection is made), and backquoted strings (see Command Substitution).
Variable substitution can be suppressed by preceding the $ with a \, except within double-quotes where it always occurs. Variable substitution is suppressed inside of single-quotes. A $ is escaped if followed by a space character, tab or newline.
Variables can be created, displayed, or destroyed using the set and unset commands. Some variables are maintained or used by the shell. For instance, the argv variable contains an image of the shell's argument list. Of the variables used by the shell, a number are toggles; the shell does not care what their value is, only whether they are set or not.
Numerical values can be operated on as numbers (as with the @ built-in command). With numeric operations, an empty value is considered to be zero. The second and subsequent words of multiword values are ignored. For instance, when the verbose variable is set to any value (including an empty value), command input is echoed on the terminal.
Command and filename substitution is subsequently applied to the words that result from the variable substitution, except when suppressed by double-quotes, when noglob is set (suppressing filename substitution), or when the reference is quoted with the :q modifier. Within double-quotes, a reference is expanded to form (a portion of) a quoted string; multiword values are expanded to a string with embedded space characters. When the :q modifier is applied to the reference, it is expanded to a list of space-separated words, each of which is quoted to prevent subsequent command or filename substitutions.
Except as noted below, it is an error to refer to a variable that is not set.
$var
${var}
$var[index]
${var[index]}
$#name
${#name}
$0
$n
${n}
$*
The modifiers :e, :h, :q, :r, :t, and :x can be applied (see History Substitution), as can :gh, :gt, and :gr. If {} (braces) are used, then the modifiers must appear within the braces. The current implementation allows only one such modifier per expansion.
The following references can not be modified with : modifiers.
$?var
${?var}
$?0
$$
$<
Command and filename substitutions are applied selectively to the arguments of built-in commands. Portions of expressions that are not evaluated are not expanded. For non-built-in commands, filename expansion of the command name is done separately from that of the argument list; expansion occurs in a subshell, after I/O redirection is performed.
A command enclosed by backquotes (`...`) is performed by a subshell. Its standard output is broken into separate words at each space character, tab and newline; null words are discarded. This text replaces the backquoted string on the current command line. Within double-quotes, only newline characters force new words; space and tab characters are preserved. However, a final newline is ignored. It is therefore possible for a command substitution to yield a partial word.
Unquoted words containing any of the characters *, ?, [ or {, or that begin with ~, are expanded (also known as globbing) to an alphabetically sorted list of filenames, as follows:
*
?
[...]
{str, str, ... }
~[user]
Only the patterns *, ? and [...] imply pattern matching; an error results if no filename matches a pattern that contains them. The `.' (dot character), when it is the first character in a filename or pathname component, must be matched explicitly. The / (slash) must also be matched explicitly.
A number of C shell built-in commands accept expressions, in which the operators are similar to those of C and have the same precedence. These expressions typically appear in the @, exit, if, set and while commands, and are often used to regulate the flow of control for executing commands. Components of an expression are separated by white space.
Null or missing values are considered 0. The result of all expressions is a string, which can represent decimal numbers.
The following C shell operators are grouped in order of precedence:
(...)
>~
!
* / %
+ -
<< >>
< > <= >=
== != =~ !~
&
^
|
&&
||
The operators: ==, !=, =~, and !~ compare their arguments as strings; other operators use numbers. The operators =~ and !~ each check whether or not a string to the left matches a filename substitution pattern on the right. This reduces the need for switch statements when pattern-matching between strings is all that is required.
Also available are file inquiries:
-rfilename
-wfilename
-xfilename
-efilename
-ofilename
-z filename
-ffilename
-dfilename
If filename does not exist or is inaccessible, then all inquiries return false.
An inquiry as to the success of a command is also available:
{ command}
The shell contains a number of commands to regulate the flow of control in scripts and within limits, from the terminal. These commands operate by forcing the shell either to reread input (to loop), or to skip input under certain conditions (to branch).
Each occurrence of a foreach, switch, while, if...then and else built-in command must appear as the first word on its own input line.
If the shell's input is not seekable and a loop is being read, that input is buffered. The shell performs seeks within the internal buffer to accomplish the rereading implied by the loop. (To the extent that this allows, backward goto commands succeeds on nonseekable inputs.)
If the command is a C shell built-in command, the shell executes it directly. Otherwise, the shell searches for a file by that name with execute access. If the command name contains a /, the shell takes it as a pathname, and searches for it. If the command name does not contain a /, the shell attempts to resolve it to a pathname, searching each directory in the path variable for the command. To speed the search, the shell uses its hash table (see the rehash built-in command) to eliminate directories that have no applicable files. This hashing can be disabled with the -c or -t, options, or the unhash built-in command.
As a special case, if there is no / in the name of the script and there is an alias for the word shell, the expansion of the shell alias is prepended (without modification) to the command line. The system attempts to execute the first word of this special (late-occurring) alias, which should be a full pathname. Remaining words of the alias's definition, along with the text of the input line, are treated as arguments.
When a pathname is found that has proper execute permissions, the shell forks a new process and passes it, along with its arguments, to the kernel using the execve() system call (see exec(2)). The kernel then attempts to overlay the new process with the desired program. If the file is an executable binary (in a.out(4) format) the kernel succeeds and begins executing the new process. If the file is a text file and the first line begins with #!, the next word is taken to be the pathname of a shell (or command) to interpret that script. Subsequent words on the first line are taken as options for that shell. The kernel invokes (overlays) the indicated shell, using the name of the script as an argument.
If neither of the above conditions holds, the kernel cannot overlay the file and the execve() call fails (see exec(2)). The C shell then attempts to execute the file by spawning a new shell, as follows:
The shell normally ignores QUIT signals. Background jobs are immune to signals generated from the keyboard, including hangups (HUP). Other signals have the values that the C shell inherited from its environment. The shell's handling of interrupt and terminate signals within scripts can be controlled by the onintr built-in command. Login shells catch the TERM signal. Otherwise, this signal is passed on to child processes. In no case are interrupts allowed when a login shell is reading the .logout file.
The shell associates a numbered job with each command sequence to keep track of those commands that are running in the background or have been stopped with TSTP signals (typically Control-z). When a command or command sequence (semicolon separated list) is started in the background using the & metacharacter, the shell displays a line with the job number in brackets and a list of associated process numbers:
[1] 1234
To see the current list of jobs, use the jobs built-in command. The job most recently stopped (or put into the background if none are stopped) is referred to as the current job and is indicated with a `+'. The previous job is indicated with a `-'. When the current job is terminated or moved to the foreground, this job takes its place (becomes the new current job).
To manipulate jobs, refer to the bg, fg, kill, stop, and % built-in commands.
A reference to a job begins with a `%'. By itself, the percent-sign refers to the current job.
% %+ %%
%-
%j
%?string
A job running in the background stops when it attempts to read from the terminal. Background jobs can normally produce output, but this can be suppressed using the `stty tostop' command.
While running interactively, the shell tracks the status of each job and reports whenever the job finishes or becomes blocked. It normally displays a message to this effect as it issues a prompt, in order to avoid disturbing the appearance of your input. When set, the notify variable indicates that the shell is to report status changes immediately. By default, the notify command marks the current process; after starting a background job, type notify to mark it.
Built-in commands are executed within the C shell. If a built-in command occurs as any component of a pipeline except the last, it is executed in a subshell.
:
alias [ name [ def ] ]
bg [ %job ... ]
break
breaksw
case label:
cd [dir ]
chdir [dir ]
continue
default:
dirs [-l]
echo [-n] list
eval argument...
exec command
exit [(expr)]
fg [%job ]
foreach var(wordlist)
...
end
The built-in command continue can be used to terminate the execution of the current iteration of the loop and the built-in command break can be used to terminate execution of the foreach command. When this command is read from the terminal, the loop is read once prompting with ? before any statements in the loop are executed.
glob wordlist
gotolabel
hashstat
history [-hr] [n]
-r
-h
if (expr )command
if (expr) then
...
else if (expr2) then
...
else
...
endif
jobs [-l]
-l
kill [sig ] [ pid ] [ %job ] ...
kill -l
-l
limit [-h] [resource [max-use ] ]
-h
resource is one of:
cputime
filesize
datasize (heapsize)
stacksize
coredumpsize
descriptors
memorysize
max-use is a number, with an optional scaling factor, as follows:
nh
nk
nm
mm:ss
Example of limit: To limit the size of a core file dump to 0 Megabytes, type the following:
limit coredumpsize 0M
login [username| -p ]
-p
logout
nice [+n |-n ] [command ]
+n
-n
nohup [command ]
notify [%job] ...
onintr [-| label]
popd [+n ]
+n
pushd [+n |dir]
+n
dir
rehash
repeat count command
set [var [= value ] ]
set var[n] = word
word
(wordlist)
Values are command and filename expanded before being assigned. The form setvar[n] = word replaces the n'th word in a multiword value with word.
setenv [VAR [word ] ]
The environment variables LC_CTYPE, LC_MESSAGES, LC_TIME, LC_COLLATE, LC_NUMERIC, and LC_MONETARY take immediate effect when changed within the C shell.
If any of the LC_* variables (LC_CTYPE, LC_MESSAGES, LC_TIME, LC_COLLATE, LC_NUMERIC, and LC_MONETARY) (see environ(5)) are not set in the environment, the operational behavior of csh for each corresponding locale category is determined by the value of the LANG environment variable. If LC_ALL is set, its contents are used to override both the LANG and the other LC_* variables. If none of the above variables is set in the environment, the "C" (U.S. style) locale determines how csh behaves.
LC_CTYPE
LC_MESSAGES
LC_NUMERIC
shift [variable ]
source [-h] name
-h
stop %jobid ...
stop pid ...
suspend
switch (string)
case label:
...
breaksw
...
default:
...
breaksw
endsw
time [command ]
example %time ls -R 9.0u 11.0s 3:32 10% 0+0k 0+0io 0pf+0w
(See the Environment Variables and Predefined Shell Variables sub-section on the time variable.)
umask [value ]
unalias pattern
unhash
unlimit [-h] [resource ]
-h
unset pattern
unsetenv variable
wait
while (expr)
...
end
% [job ] [&]
@ [var =expr]
@ [var[n]=expr]
If the expression contains the characters >, <, &, or |, then at least this part of expr must be placed within parentheses.
The operators *=, +=, and so forth, are available as in C. The space separating the name from the assignment operator is optional. Spaces are, however, mandatory in separating components of expr that would otherwise be single words.
Special postfix operators, ++ and --, increment or decrement name, respectively.
Unlike the Bourne shell, the C shell maintains a distinction between environment variables, which are automatically exported to processes it invokes, and shell variables, which are not. Both types of variables are treated similarly under variable substitution. The shell sets the variables argv, cwd, home, path, prompt, shell, and status upon initialization. The shell copies the environment variable USER into the shell variable user, TERM into term, and HOME into home, and copies each back into the respective environment variable whenever the shell variables are reset. PATH and path are similarly handled. You need only set path once in the .cshrc or .login file. The environment variable PWD is set from cwd whenever the latter changes. The following shell variables have predefined meanings:
argv
cdpath
cwd
echo
fignore
filec
EOT
ESC
hardpaths
histchars
history
home
ignoreeof
nobeep
noclobber
noglob
nonomatch
notify
path
prompt
The setting of $prompt has three meanings:
$prompt not set
$prompt set but == ""
$prompt set and != ""
savehist
shell
status
time
%D
%E
%F
%I
%K
%M
%O
%P
%S
%U
%W
%X
The default summary display outputs from the %U, %S, %E, %P, %X, %D, %I, %O, %F, and %W tags, in that order.
verbose
See largefile(5) for the description of the behavior of csh when encountering files greater than or equal to 2 Gbyte (2^31 bytes).
~/.cshrc
~/.login
~/.logout
~/.history
/usr/bin/sh
/tmp/sh*
/etc/passwd
See attributes(5) for descriptions of the following attributes:
|
bc(1), echo(1), limit(1), login(1), ls(1), more(1), pfcsh(1), pfexec(1), ps(1), sh(1), shell_builtins(1), tset(1B), which(1), df(1M), swap(1M), sysdef(1M), access(2), exec(2), fork(2), pipe(2), a.out(4), ascii(5), attributes(5), environ(5), largefile(5), termio(7I)
You have stopped jobs.
The use of setuid shell scripts is strongly discouraged.
Words can be no longer than 1024 bytes. The system limits argument lists to 1,048,576 bytes. However, the maximum number of arguments to a command for which filename expansion applies is 1706. Command substitutions can expand to no more characters than are allowed in the argument list. To detect looping, the shell restricts the number of alias substitutions on a single line to 20.
When a command is restarted from a stop, the shell prints the directory it started in if this is different from the current directory; this can be misleading (that is, wrong) as the job might have changed directories internally.
Shell built-in functions are not stoppable/restartable. Command sequences of the form a b c are also not handled gracefully when stopping is attempted. If you suspend b, the shell never executes c. This is especially noticeable if the expansion results from an alias. It can be avoided by placing the sequence in parentheses to force it into a subshell.
Commands within loops, prompted for by ?, are not placed in the history list.
Control structures should be parsed rather than being recognized as built-in commands. This would allow control commands to be placed anywhere, to be combined with |, and to be used with & and ; metasyntax.
It should be possible to use the : modifiers on the output of command substitutions. There are two problems with : modifier usage on variable substitutions: not all of the modifiers are available, and only one modifier per substitution is allowed.
The g (global) flag in history substitutions applies only to the first match in each word, rather than all matches in all words. The common text editors consistently do the latter when given the g flag in a substitution command.
Quoting conventions are confusing. Overriding the escape character to force variable substitutions within double quotes is counterintuitive and inconsistent with the Bourne shell.
Symbolic links can fool the shell. Setting the hardpaths variable alleviates this.
It is up to the user to manually remove all duplicate pathnames accrued from using built-in commands as
set path = pathnames
or
setenv PATH = pathnames
more than once. These often occur because a shell script or a .cshrc file does something like
`set path=(/usr/local /usr/hosts $path)'
to ensure that the named directories are in the pathname list.
The only way to direct the standard output and standard error separately is by invoking a subshell, as follows:
command > outfile ) >& errorfile
Although robust enough for general use, adventures into the esoteric periphery of the C shell can reveal unexpected quirks.
If you start csh as a login shell and you do not have a .login in your home directory, then the csh reads in the /etc/.login.
When the shell executes a shell script that attempts to execute a non-existent command interpreter, the shell returns an erroneous diagnostic message that the shell script file does not exist.
As of this writing, the time built-in command does not compute the last 6 fields of output, rendering the output to erroneously report the value 0 for these fields:
example %time ls -R 9.0u 11.0s 3:32 10% 0+0k 0+0io 0pf+0w
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |