UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
(C) Doug Bowman, Virginia Tech, Shell characteristics Command line interface between the user and the system Is simply a program that automatically starts when you login Waits for user to type in commands
(C) Doug Bowman, Virginia Tech, Main shell features Interactivity aliases file-name completion Scripting language Allows programming (shell scripting) within the shell environment Uses variables, loops, conditionals, etc. Next lecture
(C) Doug Bowman, Virginia Tech, Various UNIX shells sh (Bourne shell, original UNIX shell) ksh (Korn shell) csh (C shell, developed at Berkeley) tcsh bash (Bourne again SHell) … Differences mostly in level of interactivity support and scripting details
(C) Doug Bowman, Virginia Tech, The Bourne again SHell (bash) We will be using bash and ksh as the standard shells for this class This will be important for shell scripting assignments Superset of the Bourne shell (sh) Borrows features from sh, csh, tcsh & ksh Created by Free Software Foundation
(C) Doug Bowman, Virginia Tech, Changing your shell On most UNIX machines (and lab): which bash (note path) chsh On the some machines: which ksh (note path /bin/bash) ypchsh
(C) Doug Bowman, Virginia Tech, Environment variables A set of variables the shell uses for certain operations Variables have a name and a value Current list can be displayed with the env command A particular variable’s value can be displayed with echo $
(C) Doug Bowman, Virginia Tech, Some Environment variables Some interesting variables: HOME, PATH, PS1, USER, HOSTNAME, PWD $HOME /home/gradstudents/m/miali $PATH /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin $PS1 $USER sgifford $HOSTNAME avocado.cslab.vt.edu $PWD /home/grads/sgifford
(C) Doug Bowman, Virginia Tech, Setting environment variables Set a variable with = Examples: TERM=vt100 PS1=myprompt> PS1=“multiple word prompt> “ PATH=$PATH:$HOME/bin PATH=$PATH:~ DATE=`date`
(C) Doug Bowman, Virginia Tech, Aliases Aliases are used as shorthand for frequently- used commands Syntax: alias = Examples: alias ll=“ls -lF” alias la=“ls -la” alias m=more alias up=“cd..” alias prompt=“echo $PS1”
(C) Doug Bowman, Virginia Tech, Repeating commands Use history to list the last 16 commands Use fc -l to list previously typed commands m through n
(C) Doug Bowman, Virginia Tech, Editing on the command line Some command lines can be very long and complicated - if you make a mistake you don’t want to start all over again You can interactively edit the command line in several ways if using ksh set -o vi allows you to use vi commands to edit the command line set -o vi-tabcomplete also lets you complete commands/filenames by entering a TAB
(C) Doug Bowman, Virginia Tech, Login scripts You don’t want to enter aliases, set environment variables, set up command line editing, etc. each time you log in All of these things can be done in a script that is run each time the shell is started
(C) Doug Bowman, Virginia Tech, Login scripts (continued) For bash, order of files is: /etc/profile ~/.bash_profile ~/.bash_login ~/.profile After logout ~/.bash_logout
(C) Doug Bowman, Virginia Tech, Example.bash_profile (partial) #.bash_profile: executed by bash(1) for login shells umask 022 # include.bashrc if it exists if [ -f ~/.bashrc ]; then source ~/.bashrc fi # some ls aliases alias ll='ls -l' alias la='ls -A' alias l='ls -CF'
(C) Doug Bowman, Virginia Tech, Login scripts (continued) For ksh, login shells execute: ~/.profile If ENV is set: That file is executed for each new terminal Example: ENV=$HOME/.kshrc EXPORT ENV
(C) Doug Bowman, Virginia Tech, Background processing Allows you to run your programs in the background emacs vi_practice_edited &
(C) Doug Bowman, Virginia Tech, stdin, stdout, and stderr Each shell (and in fact all programs) automatically open three “files” when they start up Standard input (stdin): Usually from the keyboard Standard output (stdout): Usually to the terminal Standard error (stderr): Usually to the terminal Program command stdin stdout Programs use these three files when reading (e.g. cin, writing (e.g. cout ), or reporting errors/diagnostics