Environment Variables (35.3) Setting Up Your Environment ● ● Environment Variables ● Aliases ● ● Setup Files – .login – .tcshrc – other .*rc files ● History ● Command Completion ● ● Unlike a regular shell variable, an environment variable is inherited by another shell or program that you start. A Unix process cannot change its parent's environment. Environment variables hold information that you would normally have to commonly specify in shell commands or for Unix utilities. Environment information is passed to a C program. int main(int argc, char *argv[], char *env[]); Setting Environment Variables (35.3) ● General form. In the csh or tcsh, use the following command to set an environment variable. The general convention is to use capital letters for environment variables and lowercase letters for regular shell variables. Unsetting Environment Variables ● ● setenv <name> <value> ● Examples: setenv EDITOR vi setenv DISPLAY lov301:0.0 Sometimes it is desirable to undefine an enviroment variable. You can do this with the unsetenv command. General form. unsetenv <name> ● Examples: unsetenv EDITOR unsetenv DISPLAY Displaying the Value of an Environment Variable Predefined Environment Variables (35.5) ● ● The setenv command by itself will display the values of all of your environment variables. setenv ● You can also print out the variables using a printenv command. printenv [<environment variable name>] ● You can always display a specific environment variable using: echo $<environment variable name> Predefined Environment Variables (cont.) – SHELL: Contains the absolute pathname of your shell. – USER: Contains your username. – TERM: Contains the name of your terminal type. – DISPLAY: Used by the X Window system to identify the display server. Predefined means that their name and use is predefined. You still have to usually define them yourself. – PATH: Contains a list of directories separated by colons in which the shell looks to find commands. – EDITOR: Name of your favorite editor. – PRINTER: Name of your default printer. – PWD: Contains the absolute pathname of your current directory. Set by the cd command. – HOME: Contains the absolute pathname of your home directory. Aliases (29.2, 29.3) ● ● The alias facility allows you to define abbreviations for commands. General form. alias <name> <string> ● Examples: alias lprd “lpr -Z duplex” alias ls “ls -F” alias web “ssh websrv.cs.fsu.edu” Which (2.6, 35.27) ● The which command can be used to print out the value of an alias or the pathname of a utility. which ls which sort ● Related commands: Removing Aliases ● ● ● unalias web Source Command (35.29) ● ● General form. set prompt = <string> ● Examples: set prompt = “csh% ” set prompt = “`hostname`% “ Examples: unalias ls Setting Your Command Prompt (4.1) You can set your prompt at the command line. General form. unalias <name> whatis – print a one line summary of the command whereis – locate the binary, source, and man pages for a command alias – without the string definition, it prints the current alias for a command ● The unalias command allows you to remove aliases that were previously specified. ● It would be desirable to create a shell script that defines your environment variables and aliases. But invoking a shell script creates a child process and thus the script cannot update the environment variables or aliases of the parent process. The source command (csh and tcsh) reads a script file into the current shell instead of starting a child shell. All definitions of variables and aliases are retained. source ~/.tcshrc The .login File (3.3) Shell Setup Files (3.3) ● ● ● Shell setup files are invoked automatically at the start of specific events. ● Types: The .login file is invoked in the csh or tcsh when you login to a Unix system. Tasks typically performed in a .login file include: – Setting environment variables, which are passed to subshells automatically. – .login – .tcshrc – Setting the I/O options for your terminal. – other .*rc files – Performing commands you wish to run once each time you login. ● The .tcshrc File ● ● The .tcshrc file is run any time a tcsh shell starts. If the .tcshrc file does not exist, then the .cshrc file is invoked. The tasks performed are often those that set definitions that are not passed to subshells. Tasks typically performed in a .tcshrc file include: – setting aliases – setting your command line prompt – setting the history variable (will be discussed later) Check news, mail, calendar, etc. Other .*rc Files (3.20) ● A variety of different commands use a .*rc file in your home directory to perform some type of initialization. Some applications require that you prepare the .*rc file, while others will set one up automatically for you. The more complex applications may have a setup subdirectory off of your home directory. Common .*rc Setup Files The .mailrc Setup File ● .mailrc: used with mail and related mailers ● .gvimrc: used with the gvim editor – Setup aliases for email addresses. ● .newsrc: used with news readers – Setup the default editor for your mailer. ● .procmailrc: autonomous mail processor – Specify a file that records outgoing messages. – Specify a command to print a message. – Specify a sound when you receive a message. ● ● ● .acrorc: used with the acroread pdf viewer .xfigrc: used with the xfig picture editor The .gvimrc Setup File ● .gvimrc: for use with the gvim editor (similar in purpose to the .emacs file) – Set the background of the window. – Set the size and type of the font. – Set the number of width and height in characters. – Set whether syntax highlighting is turned on or off. .mailrc: for use with mail and related mailers The .newsrc Setup File ● ● ● ● .newsrc: for use with news readers News readers allow you to read articles that are posted to particular newsgroups. One command to use is trn (threaded read news program). The .newsrc file is automatically created by trn if it does not find one. The .newsrc file is automatically read by trn and you are given the option to add new newsgroups that have been recently created. Other Common .*rc Setup Files ● History (30.1) Other common .*rc setup files: ● .pinerc – setup file the pine mailer .procmailrc – setup for the autonomous mail processor that can be used to filter spam or generate automatic replies ● ● .acrorc – setup file for the acroread pdf viewer to indicate how to display a pdf file Most shells contain a history of your most recently entered commands. You can retrieve these commands, make changes, and reexecute them. You can set the number of commands to save in your history in your .tcshrc file. set history=<number> .xfigrc – contains recently accessed files by the xfig picture editor Extracting Portions of the Last Command (30.3, 30.4, 30.8) Command History Substitution (30.5,30.7,30.8,30.9) ● There are many options for performing command history substitution. ● You can also extract portions of the last command to be used in the current command. !! repeat the last command !$ take the last argument from the previous command ^pat^rep repeat last command replacing pat with rep !^ take argument 1 from the previous command !-n repeat the last nth previous command !* take the first through the last arguments history display the history list of commands by number history num display the last num commands !num repeat the command numbered num !:n* take the arguments n through the last argument from the previous command where the first argument is numbered at 0 !pat repeat last command that starts with pat Extracting Portions of Last Command Exs # using the last argument from the previous command ps2pdf t.ps t.pdf acroread !$ # using argument 1 from the previous command cp t.c t.c.old vi !^ # using all of the arguments from the previous command gcc main.c input.c proc.c output.c lpr !* # using arguments n through the last argument chmod +x ns.sh nd.sh ir.sh pd.sh pf.sh lpr !:2* Complete Word Function (28.6) ● In the tcsh you can complete a word by pressing the TAB character if the characters typed so far are unique. – Complete the name of command that is in your path when the word is the first word on the command line. – Complete the name of a file that is in the current or specified directory. Shell Command-Line Editing (30.14) ● ● The tcsh allows you to use the arrow keys and other special characters to retrieve previous commands and to edit them. Arrow key bindings: up go back one command down go forward one command left go backward one character right go forward one character