Tcsh variables hold only strings or a set of strings. Some variables are built into the shell and can be set either by turning them on or off, such as the noclobber or filec variable. Others are assigned a string value, such as the path variable. You can create your own variables and assign them to strings or the output of commands. Variable names are case-sensitive and may contain up to 20 characters consisting of numbers, letters, and the underscore.
There are two types of variables: local and environment. The scope of a variable is its visibility. A local variable is visible to the shell where it is defined. The scope of environment variables is often called global. Their scope is for this shell and all processes spawned (started) from this shell. If a local variable is created with set -r, it will be read-only meaning that it cannot be changed or unset (tcsh).
The dollar sign ($) is a special metacharacter that, when preceding a variable name, tells the shell to extract the value of that variable. The echo command, when given the variable as an argument, will display the value of the variable after the shell has processed the command line and performed variable substitution.
The special notation $?, when prepended to the variable name, lets you know whether the variable has been set. If a one is returned, it means true, the variable has been set. If a zero is returned, it means false, the variable has not been set.
1 > set autologout 2 > set history = 50 3 > set name = George 4 > set machine = `uname -n` 5 > echo $?machine 1 6 echo $?blah 0 |
Explanation
Sets the tcsh built-in variable autologout variable to cause automatic logout after a specified time of inactivity has passed.
Sets the built-in variable history to 50 to control the number of events displayed.
Sets the user-defined variable name to George.
Sets the user-defined variable machine to the output of the UNIX command. The command is in back quotes, telling the shell to perform command substitution.
The $? is prepended to the variable name to test whether or not the variable has been set. Because the test yields a 1 (true), the variable has been set.
The $? yields 0 (false). The variable has not been set.
The built-in echo command prints its arguments to standard output. The echo allows the use of numerous escape sequences which are interpreted and displayed as tabs, newlines, form feed, etc. Table 10.17 lists the echo options and escape sequences.
The TC shell uses the style of both BSD and SVR4, but allows you to modify the behavior of the echo command by using the built-in echo_style variable; e.g., set echo_style=bsd. See Table 10.18. See the manual page for echo.
Option | Meaning |
---|---|
-n | Suppresses newline at the end of a line of output |
Escape Sequence | |
a | Alert (bell) |
Backspace | |
c | Print the line without a newline |
f | Form feed |
Newline | |
Return | |
Tab | |
v | Vertical tab |
\ | Backslash |
nn | The character whose ASCII code is nnn (octal) |
bsd | If the first argument is -n, the newline is suppressed. |
sysv | Expands escape sequences in echo strings. |
both | Both -n and escape sequences are in effect (the default). |
none | Recognizes neither sysv or bsd. |
1 > echo The username is $LOGNAME. The username is ellie. 2 > echo " Hello therec" Hello there> 3 > echo -n "Hello there" Hello there$ 4 > set echo_style=none 5 > echo " Hello therec" -n Hello therec |
Explanation
The echo command prints its arguments to the screen. Variable substitution is performed by the shell before the echo command is executed.
The echo command by default, supports escape sequences similar to those of the C programming language and used in the SVR4 version of echo. The > is the shell prompt.
With the -n option, echo displays the string without the newline.
The echo_style variable is assigned the value none. Neither the BSD -n switch nor the SVR4 escape sequences are in effect.
With the new echo style, the string is displayed.
The Gnu version of printf can be used to format printed output. It prints the formatted string, in the same way as the C printf function. The format consists of a string that may contain formatting instructions to describe how the printed output will look. The formatting instructions are designated with a % followed by specifiers (diouxXfeEgGcs), where %f would represent a floating point number and %d would represent a whole (decimal) number.
To see a complete listing of printf specifiers and how to use them, type at the command line prompt: printf --help. To see what version of printf you are using, type printf --version. If using bash 2.x, the built-in printf command uses the same format as the executable version in /usr/bin.
Format
printf format [argument…]
printf "%10.2f%5d " 10.5 25 |
Format Specifier | Value |
---|---|
" | Double quote |