Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
C shell
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Overview of the language == The C shell operates one line at a time. Each line is [[Lexical analysis|tokenized]] into a set of words separated by spaces or other characters with special meaning, including parentheses, piping and input/output redirection operators, semicolons, and ampersands. === Basic statements === A basic statement is one that simply runs a command. The first word is taken as name of the command to be run and may be either an internal command, e.g., <code>echo</code>, or an external command. The rest of the words are passed as arguments to the command. At the basic statement level, here are some of the features of the grammar: ==== Wildcarding ==== The C shell, like all Unix shells, treats any command-line argument that contains wildcard characters as a pattern and replaces it with the list of all the filenames that match (see [[globbing]]). *<code>*</code> matches any number of characters. *<code>?</code> matches any single character. *<code>[</code>...<code>]</code> matches any of the characters inside the square brackets. Ranges are allowed, using the hyphen. *<code>[^</code>...<code>]</code> matches any character ''not'' in the set. The C shell also introduced several notational conveniences (sometimes known as [[extended globbing]]), since copied by other Unix shells. *<code>abc{def,ghi}</code> is [[Alternation (string expansion)|alternation]] (aka [[Bash (Unix shell)#Brace expansion|brace expansion]]) and expands to ''abcdef'' ''abcghi''. *<code>~</code> means the current user's home directory. *<code>~user</code> means ''user'''s home directory. Multiple directory-level wildcards, e.g., "<code>*/*.c</code>", are supported. Since version 6.17.01, recursive wildcarding Γ la [[Z shell|zsh]] (e.g. "<code>**/*.c</code>" or "<code>***/*.html</code>") is also supported with the <code>globstar</code> option. Giving the shell the responsibility for interpreting wildcards was an important decision on Unix. It meant that wildcards would work with every command, and always in the same way. However, the decision relied on Unix's ability to pass long argument lists efficiently through the [[exec (system call)|exec]] system call that csh uses to execute commands. By contrast, on [[Windows]], wildcard interpretation is conventionally performed by each application. This is a legacy of MS-DOS, which only allowed a 128-byte command line to be passed to an application, making wildcarding by the DOS command prompt impractical. Although modern [[Microsoft Windows|Windows]] can pass command lines of up to roughly 32K [[Unicode]] characters, the burden for wildcard interpretation remains with the application. ==== I/O redirection ==== By default, when csh runs a command, the command inherits the csh's stdio file handles for [[stdin]], [[stdout]] and [[stderr]], which normally all point to the [[console window]] where the C shell is running. The i/o redirection operators allow the command to use a file instead for input or output. *<code>> <em>file</em></code> means stdout will be written to ''file'', overwriting it if it exists, and creating it if it doesn't. Errors still come to the shell window. *<code>>& <em>file</em></code> means both stdout and stderr will be written to ''file'', overwriting it if it exists, and creating it if it doesn't. *<code>>> <em>file</em></code> means stdout will be appended at the end of ''file''. *<code>>>& <em>file</em></code> means both stdout and stderr will be appended at the end of ''file''. *<code>< <em>file</em></code> means stdin will be read from ''file''. *<code><< <em>string</em></code> is a [[here document]]. Stdin will read the following lines up to the one that matches ''string''. Redirecting stderr alone isn't possible without the aid of a sub-shell. <syntaxhighlight lang="csh"> set filter = "$home"'/filter' mkfifo "$filter" cat "$filter" & ( ( ls /root/ || echo No access. ) > "$filter" ) >& /dev/null </syntaxhighlight> Systems supporting file descriptors as files may use the following workaround. <syntaxhighlight lang="csh"> ( ( ( echo ok ; '' ) > /dev/fd/0 ) >& /dev/null < /dev/fd/1 ) | ( echo "$<" bye ) </syntaxhighlight> ==== Joining ==== Commands can be joined on the same line. *<code>;</code> means run the first command and then the next. *<code>&&</code> means run the first command and, if it succeeds with a 0 [[return code]], run the next. *<code>||</code> means run the first command and, if it fails with a non-zero return code, run the next. ==== Piping ==== Commands can be connected using a pipe, which causes the output of one command to be fed into the input of the next. Both commands run [[Concurrency (computer science)|concurrently]]. *<code>|</code> means connect stdout to stdin of the next command. Errors still come to the shell window. *<code>|&</code> means connect both stdout and stderr to stdin of the next command. Running concurrently means "in parallel". In a [[Multi-core processor|multi-core]] (multiple processor) system, the piped commands may literally be executing at the same time, otherwise the [[Scheduling (computing)|scheduler]] in the operating system [[Time slice|time-slices]] between them. Given a command, e.g., "<code>a | b</code>", the shell creates a [[pipeline (computing)|pipe]], then starts both <code>a</code> and <code>b</code> with stdio for the two commands redirected so that <code>a</code> writes its stdout into the input of the pipe while <code>b</code> reads stdin from the output of the pipe. Pipes are implemented by the operating system with a certain amount of buffering so that <code>a</code> can write for a while before the pipe fills but once the pipe fills any new write will block inside the OS until <code>b</code> reads enough to unblock new writes. If <code>b</code> tries to read more data than is available, it will block until <code>a</code> has written more data or until the pipe closes, e.g., if <code>a</code> exits. ==== Variable substitution ==== If a word contains a dollar sign, "<code>$</code>", the following characters are taken as the name of a variable and the reference is replaced by the value of that variable. Various editing operators, typed as suffixes to the reference, allow pathname editing (e.g., "<code>:e</code>" to extract just the extension) and other operations. ==== Quoting and escaping ==== Quoting mechanisms allow otherwise special characters, such as whitespace, wildcards, parentheses, and dollar signs, to be taken as [[Literal (computer science)|literal]] text. *<code>\</code> means take the next character as an ordinary literal character. *<code>"</code>''string''<code>"</code> is a weak quote. Enclosed whitespace and wildcards are taken as literals, but variable and command substitutions are still performed. *<code>'</code>''string''<code>'</code> is a strong quote. The entire enclosed string is taken as a literal. Double quotes inside double quotes should be escaped with <code>"\""</code>. The same applies to the dollar symbol, to prevent variable expansion <code>"\$"</code>. For backticks, to prevent command substitution nesting, single quotes are required <code>"'\`'"</code>. ==== Command substitution ==== Command substitution allows the output of one command to be used as arguments to another. *<code>`</code>''command''<code>`</code> means take the output of ''command'', parse it into words and paste them back into the command line. The following is an example of nested command substitutions with [[Leaning toothpick syndrome]]: <syntaxhighlight lang="csh"> echo "`echo "\"\`"echo "\"\\\"\\\`\""echo "\"\\\"\\\\\\\"\\\\\\\`\\\"\""echo "\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\`\\\\\\\"\\\"\""echo "\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""pwd"\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\`\\\\\\\\\\\\\\\"\\\\\\\"\\\\\\\`\\\\\\\"\\\"\\\`\\\"\"\`\""`" </syntaxhighlight> ==== Background execution ==== Normally, when the C shell starts a command, it waits for the command to finish before giving the user another prompt signaling that a new command can be typed. *''command''<code> &</code> means start ''command'' in the background and prompt immediately for a new command. ==== Subshells ==== A subshell is a separate child copy of the shell that inherits the current state but can then make changes, e.g., to the current directory, without affecting the parent. *<code>( </code>''commands''<code> )</code> means run ''commands'' in a subshell. === Control structures === The C shell provides control structures for both [[Conditional (programming)|condition-testing]] and [[iteration]]. The condition-testing control structures are the if and switch statements. The iteration control structures are the while, foreach and repeat statements. ==== if statement ==== There are two forms of the [[if statement]]. The short form is typed on a single line but can specify only a single command if the expression is true. <syntaxhighlight lang="csh">if ( expression ) command</syntaxhighlight> The long form uses then, else and endif keywords to allow for blocks of commands to be nested inside the condition. <syntaxhighlight lang="csh"> if ( expression1 ) then commands else if ( expression2 ) then commands ... else commands endif </syntaxhighlight> If the else and if keywords appear on the same line, csh chains, rather than nests them; the block is terminated with a single endif. ==== switch statement ==== The switch statement compares a string against a list of patterns, which may contain wildcard characters. If nothing matches, the default action, if there is one, is taken. <syntaxhighlight lang="csh"> switch ( string ) case pattern1: commands breaksw case pattern2: commands breaksw ... default: commands breaksw endsw </syntaxhighlight> ==== while statement ==== The [[While loop|while statement]] evaluates an expression. If it is true, the shell runs the nested commands and then repeats for as long as the expression remains true. <syntaxhighlight lang="csh"> while ( expression ) commands end </syntaxhighlight> ==== foreach statement ==== The foreach statement takes a list of values, usually a list of filenames produced by wildcarding, and then for each, sets the loop variable to that value and runs the nested commands. <syntaxhighlight lang="csh"> foreach loop-variable ( list-of-values ) commands end </syntaxhighlight> ==== repeat statement ==== The repeat statement repeats a single command an integral number of times. <syntaxhighlight lang="csh">repeat integer command</syntaxhighlight> === Variables === The C shell implements both shell and [[environment variable]]s.<ref name="USDT">{{cite book | last = Troy | first = Douglas | title = UNIX Systems | publisher = Benjamin/Cumming Publishing Company | series = Computing Fundamentals | date = 1990 | pages = 25 }}</ref> Environment variables, created using the <code>setenv</code> statement, are always simple strings, passed to any [[Process (computing)|child processes]], which retrieve these variables via the [[exec (system call)#envp|<code>envp[]</code>]] argument to [[Main function (programming)|<code>main()</code>]]. Shell variables, created using the <code>set</code> or <code>@</code> statements, are internal to C shell. They are not passed to child processes. Shell variables can be either simple strings or arrays of strings. Some of the shell variables are predefined and used to control various internal C shell options, e.g., what should happen if a wildcard fails to match anything. In current versions of csh, strings can be of arbitrary length, well into millions of characters. Variables can be enlarged as needed. However, if it's desirable to work on a fixed size, the following syntax is preferred. <syntaxhighlight lang="csh"> # Creates a variable large enough to hold 1024 elements. set fixed = {,}{,}{,}{,}{,}{,}{,}{,}{,}{,} </syntaxhighlight> === Expressions === The C shell implements a 32-bit integer expression grammar with operators borrowed from C but with a few additional operators for string comparisons and filesystem tests, e.g., testing for the existence of a file. Operators must be separated by whitespace from their operands. Variables are referenced as <code>$</code>''name''. [[Operator precedence#Programming languages|Operator precedence]] is also borrowed from C, but with different [[operator associativity]] rules to resolve the ambiguity of what comes first in a sequence of equal precedence operators. In C, the associativity is left-to-right for most operators; in C shell, it is right-to-left. For example, {{column |width=60em |1= <syntaxhighlight lang="c"> // C groups from the left int i = 10 / 5 * 2; printf( "%d\n", i ); // prints 4 i = 7 - 4 + 2; printf( "%d\n", i ); // prints 5 i = 2 >> 1 << 4; printf( "%d\n", i ); // prints 16 </syntaxhighlight> |2= <syntaxhighlight lang="csh"> # C shell groups from the right @ i = 10 / 5 * 2 echo $i # prints 1 @ i = 7 - 4 + 2 echo $i # prints 1 @ i = ( 2 >> 1 << 4 ) echo $i # prints 0 </syntaxhighlight> }} The parentheses in the C shell example are to avoid having the bit-shifting operators confused as I/O redirection operators. In either language, parentheses can always be used to explicitly specify the desired order of evaluation, even if only for clarity. Return values are limited to 8-bit. For <code>exit</code> expressions, the unary negation operator can be used for 32-bit evaluation. <syntaxhighlight lang="csh"> exit ! ! 256 # Returns 1. </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
C shell
(section)
Add topic