Understanding Unix Shells: A Comprehensive Overview

Slide Note
Embed
Share

Explore the fundamentals of Unix shells, including their role as an interface between the Unix kernel and users, different shell types like Bourne, Korn, and Bourne-again shells, shell startup files, environment variables, and more. Learn how shells interpret and run commands, manage aliases, and command history. Discover how to identify your default shell and gain insights into shell programming.


Uploaded on Sep 20, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Unix Shells Readings Chapters 5, 8 and 9

  2. Overview What is Unix shell? How shell runs a command Unix shell startup files Environment variables and search path Shell aliases Shell command history Separating and grouping commands 2

  3. Unix Shells What is a Unix shell? Interface between Unix kernel and user An interactive command interpreter A programming language Many shells sh bash ksh csh tcsh 3

  4. Many Shells Bourne shell (sh) Creator Steve Bourne in the early 80 s First shell used for shell programming Most widely supported C shell (csh) Created at UCB on their Unix implementation in the early 80 s Users wanted more familiar syntax More features (for interactive uses) than sh (e.g. job control and history) 4

  5. Many Shells Korn shell (ksh) Created by David Korn in the mid 80 s Compatible with sh but having most features of csh Features history editing (a.k.a. command-line editing) Was available on System V Public-domain version is pdksh tcsh (T-Shell) has all csh features, less bugs, and added features Bourne-again shell (bash) Offered by FSF (free software foundation) Similar to ksh and csh Command-line editing Default shell on Linux 5

  6. Whats my (Default) Shell? Check your prompt Usually bash-family uses $ Usually csh-family uses % (However, usually tcsh uses >) Superuser root usually is # Check the password file grep yourusername /etc/passwd Check the SHELL environment variable echo $SHELL Other tips (can be used to determine current shell) echo $0 ps p $$ 6

  7. How Shell Interpret and Run Commands? As a command interpreter From a terminal, prints a prompt, waits for a cmd, read cmd, interprets, runs the result. Based on the command is internal or external command, shell behaves differently Shell script shell script - cmds in a file The process how shell works is the same We will study shell programming later 7

  8. Shell Internal and External Commands Internal commands built into the shell shell performs the command E.g. chdir or cd External commands A new process will be started to run an external command E.g. ls Command to determine the type of a command $ type command_name (for bash) $ builtins (for tcsh to list all built-in commands) 8

  9. Internal and External Commands Shell checks type of command a user is trying to run Check if built-in Else check if absolute path Else check alias (except original Bourne sh, which does not support alias) Check for executable in search path Search path is a list of directories a shell must check An environment variable PATH lists these directories $ echo $PATH Search path is specified in the shell start up files 9

  10. Executing External Cmd with a New Process External commands (programs) are executed through a combination of two system calls: fork - create a new child process exec tells the kernel to execute another program Calling exec replaces the calling program with the new one being called, and therefore ending the original. To avoid replacement, the programs must fork first, then call exec. 10

  11. Login Shell vs. Non-Login Shell Shells can act as: login shell Interactive non-login shell Non-interactive shell (as a shell script interpreter) Login shells and interactive shells will read special configuration/setup files loginprogram raises a flag to the shell to tell it that it s a login shell. From users perspective (not accurate/reliable) Shell started by login program is login shell Subshell started by user is non-login shell echo $SHLVL xterm -ls starts a window system login shell xterm +ls start a non-login shell 11

  12. Starting a Unix Login Shell login program sets some environment variables HOME, PATH, SHELL, TERM, USER Check the manual page of login: man login System may set some other environment variables Login program then starts your individual shell Login shells are interactive Start-up files Shell will first execute commands in its system-wide setup files (see details in the next few slides) Then it will execute commands in user personal setup files Some dot files such as .profile under user home directory Non-login shells inherit shell variables from login shell 12

  13. Bash Startup Files As a login shell (in this order) /etc/profile .bash_profile, .bash_login, and .profile (in this order, execute commands in the first file encountered) .bash_logout (when log out) As interactive non-login shell (in this order) .bashrc (typically, it is also called by login personal setup file) Normally this configuration file will call /etc/bashrc file As non-interactive shell (executing shell scripts) No startup files will be executed 13

  14. Tcsh Startup Files As login shell /etc/csh.cshrc /etc/csh.login .tcshrc, .cshrc (in this order, only execute the first encountered) .login .logout (upon logout) As interactive non-login shell /etc/csh.cshrc .tcshrc, .cshrc (in this order, only execute the first encountered) As non-interactive shell No startup files will be executed 14

  15. Whats in Shell Startup File? For login shell Sets environment variables Environment variables can be passed to subshell Sets the search path Sets the terminal type Run one or more commands that you want when you log in who, uptime, w, etc For both login and interactive shells Set aliases Aliases cannot be passed to subshell It is better to set PATH in login startup files, instead of non-login interactive startup file (.bashrc) 15

  16. Environment Variables Shell has variables associated with values Environment variables are global variables Visible to shell and child processes created by the shell Variable assignment variable=value (for bash, no space around =) set variable = value (for tcsh, spaces around = are optional) To remove a variable unset variable To make a variable global (available to child process) variable=value; export variable (for sh and bash) export variable=value (for bash) setenv variable value (for tcsh. Note there is no equal sign =) 16

  17. Search Path Shell looks in a list of dirs called search path to determine external commands to run Unix systems have standard dirs with names like /bin, /usr/bin, /usr/local/bin, that hold standard programs Search path controls what directories and in what order the shell searches for external commands Edit your shell setup file bash: ~/.bash_profile sets your env variables tcsh: ~/.login sets your env variables You can use other files depending on the shell you use, for example .profile for bash 17

  18. An Example To illustrate how manipulate search path, consider that you create a bin directory in your home to store all your scripts Make sure your directory is in your search path so that we do not need to type the complete path Add the new path in bash, PATH=$PATH:$HOME/bin In tcsh, set path = ($path ~/bin) Or setenv PATH $PATH:~/bin 18

  19. Activate Changes To make changes in the current session, you can type the following cmd at command line: To also make them available to child processes PATH=$HOME/yourchoice; export PATH (sh and bash) export PATH=$HOME/yourchoice:$PATH (bash) set path=(~/yourchoice $path) (csh/tcsh) setenv PATH ~/yourchoice:$PATH (tcsh) 19

  20. Run a Startup File in the Current Shell To activate the changes in a startup file . .bash_profile or source .bash_profile (for bash) source .tcshrc (for tcsh) You can use the same commands to run any shell scripts (not only startup files) NOTE Source a file runs the files in the current shell That is, it affects the current shell, for example, if you source a file containing search PATH, the new PATH is available to the current shell (process) This is different from starting a shell to run (interpret) a script, which will start a new process When new process terminates, the changes will not be propagated back to the parent process (shell) 20

  21. Intro to Shell Aliases Aliases facility to shorten series of cmds and even cmd arguments and options Example: ls al alias on CS account is la Available in all shells except the original Bourne shell 21

  22. Intro to Shell Aliases Common creation of aliases is in separate file for each shell that store your aliases .bash_alias .alias.csh Use the alias command to list aliases Aliases are not passed to subprocesses It is better to put aliases in a startup file that will be used by both login shell and non-login interactive shell For example, .bashrc (for bash) and .tcshrc (for tcsh) If not defined in the subshells, you ll receive a command not found 22

  23. Defining Aliases Bash syntax $ alias name=definition No space around equal sign Good practice to put the quotes around the definition alias ls='ls --color=auto' Tcsh syntax % alias name value alias ls "ls -F" To unalias an alias Use unalias [-a] name (for bash) Use unalias pattern (for tcsh) Note that pattern can be * , which will remove all aliases 23

  24. Per-Host Shell Setup Files You may want to have different setup files for different machines For bash export HOST= `uname n` If [-e ~/.bashrc.$HOST]; then source ~/.bashrc.$HOST fi For C-Shell setenv HOST `uname n` if (-e ~/.cshrc.$HOST) then source ~/.cshrc.$HOST endif 24

  25. Important Before you make any changes Make sure you have a separate login session first In case the new changes prevent you from logging into the system again, you can use this session to change back 25

  26. Unix Shell Command History history lists the past commands with an identifying number To enable history, in bash (in a startup file) Modify variable HISTSIZE; For example HISTSIZE=1000 To enable history, in tcsh (in a startup file) set history=n, where n is the number of past cmds For example set history=100 26

  27. History in a Nutshell Shells bash and tcsh save copies of the previous command lines you type. History substitution ask for a copy of some or all of a previous command line You can think of it like variable substitution ($varname) or command substitution (`command`): the shell replaces what you type with something else Event designator Specifies a command in the history list Word designator Specifies a word or series of words from an event 27

  28. Common History Event Designators !! repeats the last command !str refer to most recent command starting with str !n - refer to command number n in history list !?str? - refer to the most recent command containing str Check more: man history Examples $ls l $!! ls -l # last command executed 28

  29. Common History Word Designators Referring to words in an event Words are numbered from 0 (the command name) to n (the last word in the corresponding command) General format Event designator, followed by a colon, followed by a number For example: !5:1 Special word designators (of last event) !$: last word !^: first word (after command name) 29

  30. History Word Designator Example: !$ take the last thing on the previous command line % tar xzf prog.tar.gz foo.c % cp i !$ !$.orig % vi letter.txt % ispell !$ % mv foo.c ~/test % cd !$ 30

  31. Use !$ for safety with wildcards $ ls a* a1 a2 a3 $ rm !$ Using the history mechanism to grab the previous cmd s args is a nice way to prevent mistakes 31

  32. Common Word Designators n: nth word ^: first word $: last word n*: all words from nth word *: all words except command, same as 1* 32

  33. Separating Commands We need to separate commands from one another For both interactive and shell programming <newline> separator Separates commands and initiates execution of command on current line Semicolon ; separator Separating multiple commands on single line ls; ps (two commands on same line) Backslash \ continues a line ls; \ # type \ ps Single ampersand (&) cmd1 & cmd2 (run cmd1 in background, then run cmd2) Or and and (|| and &&) cmd 1 || cmd2 (only run cmd2 if cmd1 fails) cmd1 && cmd2 (only run cmd2 if cmd1 succeeds) 33

  34. Grouping Commands Grouping commands use parentheses () Starts a sub-shell to run commands in group (cd test; ls) Both cd and ls run in a sub-shell They do not affect current shell (including variable assignment etc) Grouping commands use curly braces {} Do not start a sub-shell, commands run in the current shell { cd test; ls; } # supported by bash Note that the last semi-colon is required (or you can use a newline) 34

  35. Special Variables $?: exit status of previous command 0 means the command runs successfully Otherwise, there is some error $$: process ID of the current process $0: name of current script $1, to $9: first nine command-line arguments (if any) ${n}: nth command-line argument, if more than 1 digit 35

  36. Manipulating Directory Stack pushd directory_name Add a directory into a stack (and switch to that directory) pushd Switch between the top two directories in the stack popd Remove top directory and move to the new top directory dirs Display directories in stack pushd and popd can also take an integer value to specify which directory to work on For example, pushd +1 In tcsh, you can also set variable savedirs to save stack when you log out. 36

  37. I/O Redirection You can redirect the standard input, standard output, and standard error of a command or program, e.g. ls > ls.out (standard out of ls sent to file ls.out) ls 2> ls.out (standard error) ls >> ls.out (append) cat < ls.out (standard input of cat comes from file ls.out) 37

  38. Pipes Chaining multiple commands/programs together, in terms of their standard input and output For example who | sort The standard output of command who is redirected as the standard input of command sort 38

  39. Job Control Foreground vs background jobs Appending & at the end of a command line to make it background Shell wait foreground job to complete before showing prompt for user to enter the next command Shell will not wait a background job to complete, it will show the prompt and user can enter next command sleep 60 vs. sleep 60 & Listing background jobs Command jobs To bring a background job to foreground Command fg 39

  40. Reading Assignment Chapter 10 40

Related