Fall 2006 Setting up your environmentSetting up your environment☞ Environment variables – these variables are passed tochild processes☞ Aliases – modify the meaning of “commands”☞ History – a record of your shell commands☞ Command completion – lets you save keystrokesCOP 4342Fall 2006 Setting up your environmentEnvironmental variables☞ Environmental variables are passed to child processesat invocation. (The child process can of course ignorethem if it likes.)☞ Children cannot modify parent’s environmentalvariables – any modification by a child process arelocal to the child and any children it might create.COP 4342Fall 2006 Setting up your environmentEnvironmental variables☞ The traditional C “m ain” is usually defined somethinglike:int main(int argc, char *argv[], char *envp[])COP 4342Fall 2006 Setting up your environmentSetting environmental variablesCSH/TCSH: setenv VARIABLE VALUEBASH: export VARIABLE=VALUEold SH: VARIABLE=VALUE ; export VARIABLENote: there are a few special variables such as pathand home that CSH/TCSH autosynchronizes between thetwo values.COP 4342Fall 2006 Setting up your environmentSetting environmental variables[langley@sophie 2006-Fall]$export VAR1=value[langley@sophie 2006-Fall]$ bash[langley@sophie 2006-Fall]$ echo $VAR1value[langley@sophie 2006-Fall]$ exitexit[langley@sophie 2006-Fall]$ csh[langley@sophie 2006-Fall]$ echo $VAR1valueCOP 4342Fall 2006 Setting up your environmentSetting environmental variables[langley@sophie 2006-Fall]$ csh[langley@sophie 2006-Fall]$ setenv VAR2 bigvalue[langley@sophie 2006-Fall]$ csh[langley@sophie 2006-Fall]$ echo $VAR2bigvalue[langley@sophie 2006-Fall]$ exit[langley@sophie 2006-Fall]$ exit[langley@sophie 2006-Fall]$ bash[langley@sophie 2006-Fall]$ echo $VAR2bigvalueCOP 4342Fall 2006 Setting up your environmentUnsetting environmental variablesCSH/TCSH: unsetenv VARSH/BASH: unset VARYou can also leave it as local variable in bask withexport -n VAR.COP 4342Fall 2006 Setting up your environmentUnsetting environmental variables[langley@sophie 2006-Fall]$ csh[langley@sophie 2006-Fall]$ setenv VAR99 testvar[langley@sophie 2006-Fall]$ csh[langley@sophie 2006-Fall]$ echo $VAR99testvar[langley@sophie 2006-Fall]$ unsetenv VAR99[langley@sophie 2006-Fall]$ echo $VAR99VAR99: Undefined variable.[langley@sophie 2006-Fall]$ exit[langley@sophie 2006-Fall]$ exit[langley@sophie 2006-Fall]$ echo $VAR99testvarCOP 4342Fall 2006 Setting up your environmentUnsetting environmental variables[langley@sophie 2006-Fall]$ export VAR50=test[langley@sophie 2006-Fall]$ bash[langley@sophie 2006-Fall]$ echo $VAR50test[langley@sophie 2006-Fall]$ unset VAR50[langley@sophie 2006-Fall]$ echo $VAR50[langley@sophie 2006-Fall]$ exitexit[langley@sophie 2006-Fall]$ echo $VAR50test[langley@sophie 2006-Fall]$ export -n VAR50[langley@sophie 2006-Fall]$ echo $VAR50test[langley@sophie 2006-Fall]$ bashCOP 4342Fall 2006 Setting up your environment[langley@sophie 2006-Fall]$ echo $VAR50COP 4342Fall 2006 Setting up your environmentDisplaying your environmentBASH: env, printenv, set, declare -x, typeset-xCSH: env, printenv, setenvCOP 4342Fall 2006 Setting up your environmentPredefined environmental variablesWhat is “predefined” is not so much the value of thevariable as its name and its normal use.☞ PATH : a list of directories to visit. They are delimitedwith “:”. Note that csh/tcsh “autosynchronize” thisvariable.☞ EDITOR : the default editor to s tart when you run aprogram that involves editing a file, such as crontab-e.COP 4342Fall 2006 Setting up your environment☞ PRINTER : the default printer to send to.☞ PWD : your present working directory.☞ HOME : your home directory.☞ SHELL : the path to your current shell. (Be cautiouswith this one: in some shells, it is instead shell).☞ USER : your username.☞ TERM : your terminal type.☞ DISPLAY : used by programs to find the X server toCOP 4342Fall 2006 Setting up your environmentdisplay their windows.COP 4342Fall 2006 Setting up your environmentAliasesAn alias allows you to abbreviate a command. Forinstance, instead of using /bin/ls -al, you m ightabbreviate it to ll with:SH/BASH: alias ll=‘‘/bin/ls -al’’CSH/TCSH: alias ll ‘‘/bin/ls -al’’COP 4342Fall 2006 Setting up your environmentRemoving aliasesYou can remove an alias with unalias.Example:unalias llCOP 4342Fall 2006 Setting up your environmentwhich, whatis, whereis, locateThe program (or built-in) which simply gives you thepath to the named executable as it would be interpretedby your shell invoking that executable, and is created byexamining your path.The program locate looks in a database for allaccessible files in the filesystem that contain the substringyou specify. You can also specify a regular express ion,such aslocate -r ’ab.*ls’COP 4342Fall 2006 Setting up your environmentThe program whatis will give you the description linefrom the man page for the comm and you specify. (N.B.:You can also search the man page descriptions with man-k keyword.)The program whereis will give you both the path tothe exe cutable named and the page to its manpage.COP 4342Fall 2006 Setting up your environmentSetting your promptSH/BASH: PS1=’% ’CSH/TCSH: set prompt=’% ’COP 4342Fall 2006 Setting up your environment“Sourcing” commandsBecause ordinarily running a shell script means firstforking a child proce ss and then exec-ing the script in thatchild shell, it is not possible to modify the current shell’senvironmental variables from just running a script.Instead, we do what is called “sourcing” the script,which means simply executing its commands (such assetting environmental variables) inside the current shellprocess.COP 4342Fall 2006 Setting up your environmentCSH/TCSH: source FILESH/BASH: . FILEN.B.: modern versions of bash also support the sourcebuilt-in.COP 4342Fall 2006 Setting up your environment.login , .profileWhen you login, your user shell is started with -l. Forsh/bash, this means that shell will source your .profilefile; for csh/tcsh, this means sourcing your .login file.Typically, you would want your environmental variablesin that file, and any other one-tim e comm ands that youwant to do when logging in, such as checking for newemail.COP 4342Fall 2006 Setting up your environmentShell .*rc filesFor each s hell that you start, generally a series of “runcommand” files, abbreviated as “rc” will be sourced. Inthese you can set up aliases and variables that you wantfor every
View Full Document