<<

Chapter 9 The – Customizing the Environment

Tien-Hsiung Weng 翁添雄 [email protected] Objectives

• Know the difference between local and environment variables • Examine , SHELL, , etc • Use the mechanism to recall, edit, and run previously execute commands • Prevent accidental overwriting of files and logging out using set –o The Shell

• The shell is both an interpreter and a • When log in, an interactive shell presents a prompt and waits for requests • Shell supports , aliases, and history • An interactive shell runs a non-interactive shell when executing a shell was created by Billy Joy • To know the shell we are using: $SHELL • We can run to change the entry in /etc/ (non-) • a temporary switch by running the shell itself as a command: csh (C shell runs as a child) and (terminate C shell back to login shell) Environment variables command displays only environment variables

PATH, SHELL, HOME, , USER, and so on, are environment variables

MY_DIR=/home/eric/temp echo $MY_DIR sh echo $MY_DIR set will display the value of MY_DIR, but not env export MY_DIR (in Bourne and ) export statement enforces variable inheritance setenv MY_DIR (in C shell to enforce variable inheritance) Common Environment Variables HOME Æ Home (the a user is placed on logging in) PATH Æ List of directories searched by shell to a command LOGNAME Æ Login name of user USER Æ as above MAIL Æ Absolute pathname of user’s mailbox MAILCHECK Æ Mail checking interval for incoming mail TERM Æ Type of terminal Æ Absolute pathname of current directory (Korn and BASH only) CDPATH Æ List of directories searched by when used with a non-absolute pathname PS1 Æ Primary prompt string PS2 Æ Secondary prompt string SHELL Æ User’s login shell and one invoked by programs having shell escapes Prompt (PS1, PS2, PWD) echo $PS1 PS1=“C>” (in BASH) C> PS1=‘[$PWD]’ PS1=“\h>” (\h Æ hostname) Normally, PS2 is set to > or $ . –name a.out –mtime +365 \ >-exec {} \; PS2=“*” Command History history 5 Æ lists last 5 commands !! Æ repeats previous command !7 Æ repeats event number 7 !24:p Æ prints without executing event no 24 !gc Æ repeats last command beginning with gc !?size? Æ repeats last command with embedded string size cd !$ Æ Changes directory to last argument of previous command rm !* Æ removes file expanded from all arguments of previous command !:s/doc/bak Æ repeat previous tar command, substitute doc with bak The Directory Search Path(CDPATH)

CDPATH lets you avoid using pathnames when using cd to navigate to certain directories.

CDPATH=.:..:/home/eric pwd cd lib; pwd (shell first looks up the current directory for lib, failing it searches the parent directory(..) and then /home/eric The history variables Bash stores all previous commands in $HOME/.bash_history, BUT we can use HISTFILE to assign a different filename HISTSIZE=500 HISTFILESIZE=1000

Korn uses $HOME/.sh_history

C shell uses $HOME/.history set savehist=1000 set history=500 Tilde Substitution The ~ acts as a shorthand representation of the . A string prefixed by a ~ is interpreted as the home directory of the user. So, cd ~thweng will change directory to thweng’s home directory cd - Æ changes to previous directory cd /bin cd - (not available in BASH) Using set options The set statement by default display all variables, but it can make several environment settings with –o keyword- options

Keyword-options noclobber Æ prevents file overwriting with > and >> ignoreeof Æ prevents termination of login shell with [ctrl-d] notify Æ Notifies completion of background job immediately rather than the next prompt noglob Æ disable wild-card expansion Æ enables vi-style command-line editing emacs Æ enables emacs-style command-line editing allexport Æ exports all variables Using set options Examples: set noclobber unset noclobber set -o ignoreeof set +o ignoreeof Local and environment variables Set Setenv

Export

In C shell: %echo $path %set path = ($path /usr/local/bin)