Built into the TC shell are several variables consisting of one character. The $ preceding the character allows variable interpretation. See Table 10.21.
Variable | Example | Meaning |
---|---|---|
$?var | echo $?name | Returns 1 if variable has been set, 0 if not. |
$#var | echo $#fruit | Prints the number of elements in an array. |
$%var | echo $%name | Prints number of characters in a variable or array. |
$$ | echo $$ | Prints the PID of the current shell. |
$< | set name = $< | Accepts a line of input from user up to newline. |
$? | echo $? | Same as $status. Contains the exit status of the last command. |
$! | kill $! | Contains the process id number of the last job put in the background. |
1 >set num > echo $?num 1 2 > echo $path /home/jody/ellie /usr /bin /usr/local/bin > echo $#path 3 3 > echo $$ 245 > tcsh Start a subshell > echo $$ 248 4 > set name = $< Christy Campbell > echo $name Christy 5 > set name = "$<" Christy Campbell > echo $name Christy Campbell |
Explanation
The variable num is set to null. The $? preceding the variable evaluates to one if the variable has been set (either to null or some value), and to zero if the variable has not been set.
The path variable is printed. It is an array of three elements. The $ # preceding the variable extracts and prints the number of elements in the array.
The $$ is the PID of the current process, in this case, the C shell.
The $< variable accepts a word of input from the user up to the first space or newline, whichever comes first, and stores the word in the name variable. The value of the name variable is displayed.
The $< variable, when quoted (double quotes) accepts a line of input from the user up to, but not including, the newline, and stores the line in the name variable. The value of the name variable is displayed.
If a pathname is assigned to a variable, it is possible to manipulate the pathname variable by appending special TC shell extensions to it. The pathname is divided into four parts: head, tail, root, and extension. See Table 10.22 for examples of pathname modifiers and what they do.
Modifier | Meaning | Example | Result |
---|---|---|---|
:r | root | echo $pn:r | /home/ellie/prog/check |
:h | head | echo $pn:h | /home/ellie/prog |
:t | tail | echo $pn:t | check.c |
:e | extension | echo $pn:e | c |
:g | global | echo $p:gt | (See Example 10.75) |
1 > set pathvar = /home/danny/program.c 2 > echo $pathvar:r /home/danny/program 3 > echo $pathvar:h /home/danny 4 > echo $pathvar:t program.c 5 > echo $pathvar:e c 6 > set pathvar = ( /home/* ) echo $pathvar /home/jody /home/local /home/lost+found /home/perl /home/tmp 7 > echo $pathvar:gt jody local lost+found perl tmp |
Explanation
The variable pathvar is set to /home/danny/program.c.
When:r is appended to the variable, the extension is removed when displayed.
When :h is appended to the variable, the head of the path is displayed; that is, the last element of the path is removed.
When :t is appended to the variable, the tail end of the path (the last element) is displayed.
When :e is appended to the variable, the extension is displayed.
The variable is set to /home/*. The asterisk expands to all the pathnames in the current directory starting in /home/.
A special history modifier can be used to change the case of letters in a variable.
1 > set name = nicky > echo $name:u Nicky 2 >set name = ( nicky jake ) > echo $name:gu Nicky Jake 3 > echo $name:agu NICKY JAKE 4 > set name = ( TOMMY DANNY ) > echo $name:agl tommy danny 5 > set name = "$name:agu" > echo $name TOMMY DANNY |
Explanation
When :u is appended to the variable, the first letter in its value is uppercased.
When :gu is appended to the variable, the first letter in each word in the list of values is uppercased.
When :agl is appended to the variable, all letters in its value are lowercased.
The variable is reset with all letters in its list uppercased.
3.135.246.245