Customizing the Shell Environment
UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive: programmable shell scripts Choices of shells –Bourne- good programming features, weak interactions –C (Bill Joy at Berkeley)- good interactions, weak programming –Korn- best of both shells –Bash (GNU)- conforms to POSIX shell specification User’s shell specified in /etc/passwd. Use echo $SHELL Shell features are customizable.
Review: Exporting and Environment Variables A local variable is not visible in child processes: $ x=5 ; sh Create a child process $ echo $x No value in child! An exported variable is visible in all child processes: $ x=5 ; export x x is now environmental $ sh ; echo $x Create a child process 5 Changes made in child are not available in parent: $ x=7 Value set in child $ exit Child dies; back to parent $ echo $x 5 Original value in parent
Some Important Environment Variables SHELL – user’s login shell HOME – user’s home directory LOGNAME, USER – user’s login name PATH – list of directories to search for commands TERM – type of terminal MAIL – user’s mailbox file PS1, PS2 – primary and secondary prompt strings The set command (without parameters) displays the values of all variables (both local and environment). The env command displays the values of only the environment variables.
Aliases alias command assigns shorthand names to commands General Form: alias commandname=“command [options]” Examples: alias..=“cd..” alias search=grep alias rm="rm -i” No whitespace around “=“ Use unalias commandname to unset an assignment alias without parameters displays all assigned aliases Aliases are not supported in Bourne Shell
Aliases continued Too much aliasing can be confusing. Useful command: whence cmdname Checks whether an alias "took” returns the path when cmdname is the name of a script or executable program, or the command executed when it's an alias.
Login Scripts Profiles are scripts that are executed when a user logins. Profile is located in a file named.profile (.login for C Shell) In the user’s home directory. A typical.profile : export PATH=$PATH:$HOME/bin export TERM=vt100 stty intr ^C alias rm=“rm –i” set –o ignoreeof echo “Today is `date`” C shell also provides a logout script (.logout ) feature.
Other Shell-Specific Features Command History (not Bourne) – lets you recall previous commands and edit/ re-execute them. Example: $ history 2 display the last 2 commands 39 cp *.c../backup 40 history 2 $ !39 re-execute event 39 (C/ Bash) Whole set of history commands available for editing /repeating commands Command set is different for Korn shell (e.g., r39 re-executes event 39)
Other Shell-Specific Features Continued In-Line Command Editing (Bash only) – lets you perform vi/ emacs-like editing of commands. Use a setting like: $ set –o vi Tilde Substitution (not Bourne) – use ~ as shorthand substitution for your home directory. Examples: $ cd ~rick goes to rick’s home directory $ cd ~ also goes to rick’s home directory! $ cd ~~ goes to the directory you last cd ’d to
Other Shell-Specific Features Continued set Command (not Bourne) : set displays the values of all shell variables set -o vi set editor to vi for command line editing set –o noclobber prevent file overwriting using > and >> set –o ignoreeof prevent termination of session with ctl-d set –o notify notify immediately when a background job is done unset –o removes the option setting or set +o
Summary of Shell Features FeatureBourneCKornBash Command shcshkshbash Environment Variables exportsetenvexport AliasesN/AYes Login Files.profile.login.profile Command HistoryN/A !r! In-Line Command EditingN/A set –o vi Tilde SubstitutionN/AYes Set optionsN/AYes