Chapter 1 Introducing UNIX

Chapter 1 Introducing UNIX

Chapter 9 The Shell – Customizing the Environment Tien-Hsiung Weng 翁添雄 [email protected] Objectives • Know the difference between local and environment variables • Examine PATH, SHELL, MAIL, etc • Use the history mechanism to recall, edit, and run previously execute commands • Prevent accidental overwriting of files and logging out using set –o The Shell • The Unix shell is both an interpreter and a scripting language • When log in, an interactive shell presents a prompt and waits for requests • Shell supports job control, aliases, and history • An interactive shell runs a non-interactive shell when executing a shell script • C shell was created by Billy Joy • To know the shell we are using: echo $SHELL • We can run chsh command to change the entry in /etc/passwd (non-linux) • Make a temporary switch by running the shell itself as a command: csh (C shell runs as a child) and exit (terminate C shell back to login shell) Environment variables env command displays only environment variables PATH, SHELL, HOME, LOGNAME, 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 BASH) export statement enforces variable inheritance setenv MY_DIR (in C shell to enforce variable inheritance) Common Environment Variables HOME Æ Home dir (the directory a user is placed on logging in) PATH Æ List of directories searched by shell to locate a command LOGNAME Æ Login name of user USER Æ as above MAIL Æ Absolute pathname of user’s mailbox file MAILCHECK Æ Mail checking interval for incoming mail TERM Æ Type of terminal PWD Æ Absolute pathname of current directory (Korn and BASH only) CDPATH Æ List of directories searched by cd 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 strings (PS1, PS2, PWD) echo $PS1 PS1=“C>” (in BASH) C> PS1=‘[$PWD]’ PS1=“\h>” (\h Æ hostname) Normally, PS2 is set to > or $ find . –name a.out –mtime +365 \ >-exec rm {} \; 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 !tar:s/doc/bak Æ repeat previous tar command, substitute doc with bak The Directory Search Path(CDPATH) CDPATH environment variable 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 which 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 home directory. 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 at the next prompt noglob Æ disable wild-card expansion vi Æ 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).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    13 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us