Working with Your Shell

Shell as Processor: An Introduction Shell allows you to access Bourne family (/bin/sh, /bin/ksh, /bin/bash, /bin/zsh) or C Shell family (/bin/csh, /bin/tcsh) with zsh and bash are commonly used. Why Commands Are Noninteractive ? A command may take input from the output of another command. May be scheduled to run specific times. User input can also be provided through command line arguments. Command arguments need not be known in advance. Allows designing of applications that determine their own behavior by reading configuration files. Example of /bin/csh example‐isp> $SHELL /bin/csh example‐isp> finger ejray Login name: ejray In real life: "RayComm" Directory: /home/users/e/ejray Shell: bin/csh On since Jul 23 06:58:48 on pts/16 from calvin.raycomm.com 1 minute 28 seconds Idle No unread mail No Plan. example‐isp> Example of /bin/bash [ejr@hobbes ejr]$ echo $SHELL /bin/bash [ejr@hobbes ejr]$ finger ejr Login: ejr Name: Eric J. Ray Directory: /home/ejr Shell: /bin/bash On since Wed Jul 22 07:42 (MDT) on tty1 3 hours 15 minutes idle On since Thu Jul 23 08:17 (MDT) on ttyp0 from calvin No mail. Project: Working on UNIX VQS. Plan: This is my plan‐work all day, all night. [ejr@hobbes ejr]$ Available Shells and Making a Change [ejr@hobbes ejr]$ /etc/shells /bin/bash /bin/sh /bin/tcsh /bin/csh /bin/zsh [ejr@hobbes ejr]$ chsh Changing shell for ejr. Password: New shell [/bin/bash]: /bin/zsh Shell changed. ejr@hobbes ~ $ ejr@hobbes ~ $ su ‐ ejr Password: ejr@hobbes ~ $

Note: You may also shell name to change shell. To a shell: type exit or Ctrl+Delete Types of Commands

External program on disk which could be – a binary executable (written in C, C++). – a script (like a shell or perl script). Internal command of the shell which could be – a builtin (like , , etc.) – an defined by the user that invokes the disk or internal version in a specific manner. Basic Shell Commands

Using SHELL , e.g. > echo $SHELL Using finger userid, e.g. > finger sjkao Seeing which shell are available, e.g. cat /etc/shells Changing shell with chsh, e.g. > chsh or > /bin/zsh Exiting from a temporary subshell, e.g. > exit or crtl + delete Completion in bash and zsh, using tab key Viewing screen history, using keys:↑, ↓,←,→ Changing identity with su, or su – userid Fixing terminal settings with stty or reset The A shell variable (or environment variable) that specifies a list of directories to search. Shell looks at PATH only when command is not used with a pathname and is also not a . Command can still be executed if not in PATH by – Using a pathname. – Modifying PATH to include the directory containing the command. PATH can be modified in an absolute or relative manner: PATH=/usr/bin:. (Absolute) PATH=$PATH:/usr/local/bin (Relative) Modified setting is lost after user has logged out unless saved in a startup file. How the Shell Determines the Command to Run If command is invoked with a pathname (like /bin/echo), the shell runs program at the specified location. If command is invoked without a pathname, the shell first checks whether it is an alias or builtin: If alias or builtin, the shell runs it without looking in disk. If not, the shell looks at the PATH variable for directories where the command may reside. Flexibility of Command Usage Run multiple commands in the same line: date ; echo $PATH  a command into multiple lines: $ echo “Hello [Enter] > Dolly” [Enter] Save command output in a file (redirect): date > foo Use output of one command as input of another (pipe): date | ‐d” “ ‐f2 Run a command in the background with &: ‐lRa / > $HOME/ls‐lRa.txt & Command Classification: A Different Approach Utilities that are generally used in standalone mode (, stty, bc). Commands that do useful work but produce no output (, , ). Commands that produce output which may need further processing (date, , ls). Commands specially designed to accept output of other commands as their input and vice versa (, , ). The Wild Cards  * Any number of characters, including none  ? A single character  [x‐z] A single character within the range a‐z  {pat1, pat2, …} Match the patterns pat1, pat2, ..  [!a‐z] A single character that is not within !(flname) All except flname

 The * and ? don’t match all filenames beginning with a . and the / of a pathname. Escaping and Quoting  \* is escaping, the asterisk has to be treated and matched literally instead of being interpreted as a metacharacter.  When a command argument is enclosed in quotes, the meanings of all encloded special characters are turned off. It is quoting. Stdin, Stdout, Stderr Stdin – the file representing input, which is connected to the keyboard. File descriptor 0.  3 possible input source: the keyboard, a file (using <), or another program using a pipeline.  Stdout – the file representing output, which is connected to the display. File descriptor 1.  3 possible destinations: the terminal, a file (using> or >>), and as input to another program using a pipeline  Stderr – the file representing error messages that emanate from the command or shell. It is also connected to the display. File descriptor 2. Using Completion in the bash shell bash‐2.00$ ls Complete NewProject bogus2 ftp puppy Completed News dead.letter mail temp Mail access Files public_html testme bash‐2.00$ cd public_html/ bash‐2.00$

Note: type cd pub and press the Tab key to complete the public_html. Viewing Session History in the bash Shell [ejr@hobbes clean]$ ls background.htm info.htm logo.gif [ejr@hobbes clean]$ ls (by typing [up arrow] key to reuse the previous command) background.htm info.htm logo.gif [ejr@hobbes clean]$ history 1 free 2 id deb 3 id ejr 4 ‐a 5 ls ... 40 cd 41 cp .bash_history oldhistory 42 vi .bash_history 43 elm … [ejr@hobbes clean]$ !40 cd [ejr@hobbes ejr]$

Note: Commands of previous session are kept in the ~/.bask_history file. Changing Your Identity with su [ejr@hobbes asr]$ ls Projects testing [ejr@hobbes asr]$ su asr Password: [asr@hobbes asr]$ ls Projects testing [asr@hobbes asr]$ su ‐ ejr Password: [ejr@hobbes ejr]$ ls Mail editme script2. Projects fortunes.copy scriptextra.sed Xrootenv.0 fortunesl.txt sedtest above.Htm fortunes2.txt sorted.address.temp address.book groups temp.htm address.temp history.txt tempsort axhome html.htm html.html test2 .txt mail testing.gif clean manipulate testing.wp compression nsmail typescript [ejr@hobbes ejr]$ exit [asr@hobbes asr]$ exit [ejr@hobbes ejr]$ exit

Note: su without specifying a username, assume you are changing to root. Root Access Security and Hyphen (‐)  Be careful when you login as the root.  Use su to change to root will provide a little extra security.  if you su to another user (no hyphen) and the new user doesn’t have read and execute permissions for the current directory, you will see error message. The addition of the hyphen (‐) will force a new login shell with all environment variables and defaults according to the settings for the user you are switching to. Fixing Terminal Settings with stty  If Backspace and Delete keys are not working properly (usually occurs when using telnet), you may fix it using stty erase ‘^?’ or stty erase ‘^H’  Depending upon your terminal, you may need to type stty erase and press Backspace key, and type stty erase ‘^H’ and press Ctrl+H.  Commonly, typing stty sane will fix a lot of oddities.  reset may also correct the terminal oddities. Exiting the Shell  exit  logout  Ctrl+D