tcsh Shell Variables

Introduction

tcsh maintains shell variables that you can treat like programming language variables.

We'll discuss several interesting tcsh shell variables, including:

Notes

  • Unless you're programming shell scripts, setting your own variables is of little use.
  • tcsh pays attention to lots of specific shell variables.
  • Set shell variables with set command, remove them with unset command.
  • set with no arguments shows you all variables that are currently set.
  • The command @ does the same thing.
  • The variable tcsh will be set. If you need to, you can check in ~/.cshrc for that variable and do tcsh-dependent things only if it is set.
  • ${name} on a command line gets replaced with the value of the variable name can use just $name if surrounded by blanks
    
    % echo $path
    /usr/bin /usr/ucb /usr/local/bin /util/bin /util/X11/bin
    % 
    
    
  • ${?name} (or just $?name if surrounded by spaces) will be the value 1 if the variable is set, and 0 if the variable is not set.
  • This can be used in the ~/.cshrc file to do different things based on whether a variable is set or not.
  • The shell provides an if-then-else-endif statement. A simple use of this in a ~/.cshrc file would be:
    
    if ($?tcsh) then
            echo "Shell is tcsh"
    else
            echo "Shell is csh"
    endif 
    
    
  • If what is between the parentheses evaluates to anything but 0 it is true and the then commands are executed, otherwise it is false and the elsecommands are executed
  • If you're only running one command and you don't have an else clause, you can skip the then, else, and endif:
    
    % if ($?tcsh) echo "Shell is tcsh"
    Shell is tcsh
    % 
    
    
  • Most people have this line in their ~/.cshrc file, close to the top:
    
    if (! $?prompt) exit
    
    
  • This will stop the sourcing of ~/.cshrc if the shell variable prompt is NOT set (the "!" inside the parentheses is a logical NOT operator).
  • When you are logging in, the shell that is being started is passed some command line flags telling it that it is a login shell. The prompt shell variable will be set.
  • Many programs start up sub-shells to do certain tasks. These are not login shells. The shell will not set a prompt when running as a sub-shell.
  • Depending on your environment, you may need to put commands in ~/.cshrc that modify terminal characteristics. For example, to set the terminal so the backspace key erases:
    
    stty erase ^H
    
    
  • Commands like stty cannot be used in sub-shells, so put them after the test for the prompt shell variable.

path

  • The path variable is the list of directories that tcsh will search for commands.
  • Commands on UNIX are simply executable files, usually compiled from C source code.
  • If the path variable doesn't include the directory that rm executable file is in, then UNIX says Command not found. If you try to use the rm command:
    
    % rm file
    rm: Command not found
    % set path=(/usr/bin)
    % rm file
    %
    
    
  • Usually your sys-admin will put a reasonable setting for path in your startup file or the system-wide startup file.
  • Often programmers want to add their own directories to the path variable, usually ~/bin.
  • If your startup file doesn't have a line that sets path, then it probably gets set in a system-wide startup file.
  • In CSE Solaris systems, the system-wide startup file is /etc/.cshrc. It sets all users' default paths as:
    
    set path= (/usr/local/bin /usr/sfw/bin /util/bin /util/TeX/bin /util/elm/bin /util/X11/bin
    /usr/openwin/bin /opt/sunstudio12.1/bin /opt/SUNWspro/bin /usr/ccs/bin /usr/ucb /usr/bin
    /usr/dt/bin .)
    
    
  • This table describes the various directories included in the Solaris default path:
    Path Description
    /usr/local/bin A few local things, like tcsh.
    /util/bin Most of the non-X locally-installed packages.
    /util/TeX/bin TeX and LaTeX stuff.
    /util/lang/bin Many languages and language tools.
    /usr/openwin/bin OpenWindows executables. (Sun's X11R6)
    /util/X11/bin Other X programs.
    /util/elm/bin The ELM mail reader program.
    /opt/SUNWspro/bin The Sun compiler suite.
    /usr/ucb BSD-like Solaris executables.
    /usr/bin The bulk of the Solaris executables.
    /usr/ccs/bin Some development tools, like make, lex, strip, etc.
    /usr/dt/bin CDE executables. (Sun's Motif-based desktop)
    . The current directory.
  • If your sysadmin has already set a default path and you need to extend the path to include additional directories, it is best to extend the default path with this type of syntax:
    
    set path = ($path ~/bin)
    
    
  • This just adds the directory ~/bin to whatever the path variable was set to previously.
  • The bare minimum path setting would just have /usr/bin in it.
  • You can see the path setting with the command:
    
    % echo $path
    
    
  • Programmers tend to add the . directory as well as their own ~/bin directory so the shell finds executables in the current working directory.
  • This can be dangerous in a hostile environment--for example, at a University :-)
  • tcsh will search through path from left to right looking for executable files.
  • If you have path set to (. /usr/bin), then the shell searches the current working directory first.
  • If you cd to another user's directory where they have their own executable file named ls, you will run it instead of the normal UNIX ls command.
  • It is best to have "." at the end of your path definition if you have it in your path at all (your sys-admin may need to remove it completely for safety).
  • You can always run an executable in your current directory with ./filename (dot slash filename).
  • On Solaris, Berkeley-style commands are in the directory /usr/ucb.
  • You can set your path to (/usr/ucb /usr/bin) if you prefer the Berkeley flavor of commands.
  • If your path is not set up for you, you might want to check with your sys-admin to see where locally-added programs are kept and add that location to your path.
  • When you're adding entries to your .cshrc file to reconfigure your path variable, remember that the tcsh currently interpreting your commands read .cshrc at startup and won't read it again.
  • Your changes don't normally take effect until you log off and then log back on.
  • You can force your current tcsh to re-read your .cshrc file after you've edited it with the source command, which tells tcsh to read a file and execute commands in it:
    
    % vi .cshrc
    % source .cshrc
    
    
  • If your path gets messed up, you can always use full pathnames to executables to save yourself:
    
    % /usr/bin/vi .cshrc
    
    
  • Most system commands are in /usr/bin.
  • tcsh looks through all directories in your path when it first starts up and builds an internal list of executable files available in those directories.
  • If an executable gets added to one of the directories after you log in, you need to use the rehash command to have your shell rebuild the internal list.
  • Other commands related to paths are which and whereis.
  • which will show you the full pathname to the executable you run for a given command
    
    % echo $path
    /usr/bin /usr/ucb /usr/local/bin /util/bin
    % which ls
    /usr/bin/ls
    % set path=(/usr/ucb /usr/bin /usr/local/bin /util/bin)
    % which ls
    /usr/ucb/ls
    % alias ls 'ls -AF'
    % which ls
    ls:      aliased to ls -AF
    % 
    
    
  • whereis will look through standard system directories for commands, showing as many as it finds.
  • It also shows manual pages:
    
    % whereis ls
    ls: /usr/bin/ls /usr/ucb/ls /usr/man/man1/ls.1 /usr/man/man1b/ls.1b
    %
     
    

ignoreeof

  • The ignoreeof variable prevents you from being logged off if you type control-D (the EOF character) at the shell prompt.
  • You can set it to the number of EOFs to ignore before logging you out:
    
    % set ignoreeof=4
    % ^D
    Use "exit" to leave tcsh.
    % ^D
    Use "exit" to leave tcsh.
    % ^D
    Use "exit" to leave tcsh.
    ~% 
    
    *poof*
    
    

prompt

  • You can customize your prompt to be something other than the default.
  • Set the shell variable prompt to be what you want your prompt to be:
    
    % set prompt = "Yes dear? "
    Yes dear? 
    
    
  • You can use formatting sequences inside the prompt string that the shell will recognize and change for you.
  • Some of the more useful formatting sequences available are:
    • %/ : the current working directory
    • %~ : current working directory but uses ~ character to show a home directory
    • %c : trailing part of current working directory
    • %m : first part of computer's name
    • %t : time
    
    % set prompt="%m [%~] % "
    calvin [~] % hostname
    calvin.cse.Buffalo.EDU
    calvin [~] % pwd
    /home/kns
    calvin [~] % cd /tmp
    calvin [/tmp] % cd /usr/local
    calvin [/usr/local] % cd
    calvin [~] % 
    
    

rmstar

  • The rmstar shell variable makes tcsh prompt you before executing the potentially dangerous rm * command:
    
    /tmp% mkdir foo
    /tmp% cd foo
    foo% touch bar1 bar2 bar3
    foo% ls
    bar1  bar2  bar3
    foo% set rmstar
    foo% rm *
    Do you really want to delete all files? [n/y] y
    foo% unset rmstar
    foo% touch bar1 bar2 bar3
    foo% rm *
    foo%
    
    

term

  • Programs that need to move the cursor around on the screen (the visual editors for example) need to know what terminal type you are on.
  • Most often, the system learns this on its own. But depending on how you wind up logged in, the system may not know your proper terminal type.
  • The shell variable term should be set to the right terminal type. For example:
    
    % set term = vt220
    
    
  • See your sys-admin if you can't figure out what terminal type to set.