------------------------------------------------------------- Linux Shells by Example: Chapter 8 "First Pass" Reading Guide ------------------------------------------------------------- -IAN! idallen@ncf.ca Here is a reading guide and some review questions for a "first pass" reading of Chapter 8 "The Interactive Bash Shell": *) To what are stdin, stdout and stderr attached in an interactive shell? *) Compare the relative file sizes of /bin/bash and the Linux kernel in files /boot/vmlinuz* *) Compare the version number of Bash in the Linux lab with the version available on the ACADUNIX machine from IBM. *) Use the pstree command to trace the number of parent processes betwen your shell and the init process. Write down the names and process id numbers of each process. *) Name three things that are part of a process environment that are passed on to child processes (after a fork() system call). *) The start-up sequence of the bash shell is rather confusing. Don't try to memorize it; refer to the man page when you need to know. *) Name one file in your HOME directory that is "sourced" by the shell when it starts up. *) Skim over the explanations of the profile files in examples 8.3 to 8.7. - come back to these after you have read more of the chapter. *) What bash option prevents the shell from sourcing any of its startup files? *) Skim over all the bash options in 8.1.4. Come back to them later. - what is the syntax to turn on/off an option? *) Is the "shopt" built-in available in our version of the Bash shell? - Is it available in the version of Bash on ACADUNIX? - what is the difference between "shopt" with no arguments and "shopt -p"? *) Skim over the various shell prompt escape sequences. Come back later. - How do change your shell prompt? *) What is the purpose of the PATH variable in the shell? - does your PATH variable in the Linux lab contain a dot? - does your PATH variable on ACADUNIX contain a dot? *) If your PATH does not contain dot (the current directory), how do you execute a command in the current directory? *) Skip the hash command (p.280) on first reading. Come back later. *) What is the difference between executing a script of shell commands ("./foo") and using "source" to read the same list of commands ("source foo")? (read the little "a" footnote on p.281) *) What are tokens on a shell command line? - what delimiters separate the tokens? (the list of delimiters in the book is only partially complete) *) The command name may not be the first word on the command line. What might precede the command name? *) In which order does the shell search for a name that you type on the command line? Put these in order: builtins, keywords, executables, aliases, functions - which is processed first? - which is processed last? Note: These things nest - An alias might expand to contain a function that contains several executable command names. *) What are the differences between shell built-in commands and normal commands such as "date", "ps", "cat", etc.? *) Skim the section on "Changing the Order of Command Line Processing" ("command,builtin,enable"). Come back later. *) What is the possible range of exit statuses for a process? *) (Example 8.20) Based on a shell exit status value of 130 after a user types ^C (the Interrupt signal), what is the Unix numeric value (number) of the Interrupt signal "SIGINT"? (Hint: it's less than 10.) *) How can you run multiple independent commands on the same command line, one after the other? (This is different from a pipe, when the output of one command runs into the input of another.) *) What do the "false" and "true" commands do? (RTFM) *) What is the output of these command lines: $ true && echo ho $ false || echo hi $ grep alleni /etc/passwd || echo "NOT FOUND" $ grep NOSUCHuser /etc/passwd || echo "NOT FOUND" *) What shell variable contains the process identifier of the most recent background job? *) What is "job control"? *) (Section 8.1.7) A "job" is not the same as a "program" or "process". A job is at least one program; but, it could be several programs if they are connected together using pipes. Job control controls all the programs in the pipe. *) What is "command completion"? "filename completion"? *) What is the "history mechanism"? Skim table 8.5 - don't memorize it. *) True or False: When you have used the up-arrow to select a command from the history list, your cursor must be at the far right end of the command line before you push "enter" to re-excute the command. *) Skim over the "fc" command (p.294-297, Example 8.29, 8.30) on first reading. Come back to it later. *) Skim over the "bang! bang!" part of history (p.298-200) on first reading. Come back to it later. *) Skim over "Command line editing" (p.301-305) on first reading. Come back to it later. *) Skim over "The Readline Library" (Section 8.2.4, p.306-313) on first reading. Come back to it later. *) (Section 8.2.5) Are shell aliases inherited by child processes of the current shell? *) How can an alias be defined and saved so that all future shells will have it available? *) What is the syntax for defining an alias in the shell? *) Alias expansion happens very early on in the processing of a command line. It happens before quoted strings are collected, before semicolons are recognized, and even before redirection is removed from the command line: $ alias foo='echo "hello' $ foo there" hello there $ alias foo=date $ foo Mon Sep 16 01:00:00 EDT 2002 $ foo >out $ >out foo bash: foo: command not found $ alias foo="echo hi ; echo" $ foo bar hi bar Aliases must truly be the first word in the command being executed. *) (Section 8.2.6) How do you use the "directory stack"? (This is *very* useful for moving around between directories!) *) The book confuses metacharacters with wildcards. Wrong. "Wildcards" are a DOS term for what Unix people call "globbing", (global file matching) and therefore "wildcards" apply only to file name matching metacharacters. Backslash, ampersand, semicolon, and dollar are *not* wildcard characters; they are just shell metacharacters. Missing from the metacharacter list in table 8.14: pipe, redirect ('<' and '>'), tilde, parentheses, quotes. *) Study section 8.2.8 (Globbing) well. Read the Errata for errors in this section! *) Double-quoting turns off *most* metacharacters. Single quoting, like using backslashes, turns off all metacharacters (except the closing single quote!). *) I've never seen anyone use ~+, ~-, or $OLDPWD in scripts. (p.323) *) Do not set shell option "dotglob". (p.324) *) Skim over "Extended Filename Globbing". I've never seen it used. *) Study section 8.3 (Variables) well. Read the Errata for errors in this section! *) How much white space is allowed around the '=' in a variable assignment? *) How do you set a variable to be empty (null)? *) What distinguishes an environment variable from a local shell variable? (Which one is inherited by child processes? How can a variable be made into an environment variable?) *) Can an environment variable be named with lower-case letters? *) What does this command sequence do? $ dog=123 ; cat=dog ; cow=cat ; bat=cow $ echo $dog $cat $cow $bat $ bash <= start new shell $ echo $dog $cat $cow $bat $ exit <= return to previous shell $ export cat $ export $cat $ export $bat $ bash <= start new shell $ echo $dog $cat $cow $bat *) Can parent processes inherit environment variables from child processes? *) Example 8.59: A nested shell is *not* a subshell. A subshell is a forked copy of the current shell without an intervening exec, usually found inside parentheses: $ ( echo This is a subshell. ; date ; who ) A subshell contains all the aliases, functions, and variables of its parent (because it is an exact copy). A nested shell only contains the things that are passed in the environment across an exec() system call. *) Important environment variables (on first reading): EDITOR, HOME, MAIL, PATH, PS1, PWD, SHELL Skim over the others; come back later. *) Skim over the Table 8.20 escape sequences to "echo". Come back to this later. *) Skim over the details of "printf" on first reading. Come back to this later. *) Skim over the details of "Variable Expansion Modifiers" (p.340-344). The most common use is shown in Line 6 in Example 8.64; read and understand this one usage. *) Skim over the details of "Variable Expansion of Substrings" (p.344-346). Come back to this later. *) What are "positional parameters"? *) What is in positional parameter zero ($0)? *) What is special about $@ used inside double quotes? (This is the only time a double-quoted string will turn into multiple arguments. Normally, the double-quotes hide embedded blanks and thus the interpolated string remains as one single argument. This is *NOT* true when the string is "$@".) *) Important special shell variables to know: $0 $1 $# $* $@ $$ $? $! Memorize the meanings of these special variables. *) Study section 8.3.3 (Quoting) very, very carefully. Also read Appendix C, and the extra notes on quoting. See the quotes.txt file for examples and exercises. Read the Errata for errors in these sections! *) (Section 8.3.4) Use the new form of Command Substitution (Example 8.80) in your scripts. You will find old scripts, both Bourne shell and C shell, using the old form. *) In 8.3.6 items 3-6 are done *at the same time*, left-to-right, not one after the other. You can't have parameter that contains a variable, or a variable that contains a command substitution, or a command substitution that contains an arithmetic expansion. All these are expanded simultaneously, left to right on the line. *) Skim over Section 8.3.7 (Arrays). Come back to it later. *) (Section 8.3.8) How do you define a function in the shell? The "old" first syntax under FORMAT on p.360 is the one understood by both Bourne shells and Bash shells - use it and your functions will work everywhere: $ myfunc () { echo "Hello world." ; } $ myfunc Hello world. *) How do you refer to the first argument on the command line from inside a function? *) Study section 8.3.9 (Standard I/O and Redirection) very, very carefully. Concentrate on stdin (fd unit 0), stdout (fd unit 1), and stderr (fd unit 2). The other stuff with fd 3 (Example 8.90, etc.) can be learned later. *) Study section 8.3.10 (Pipes) very, very carefully. *) (Section 8.3.11) What is a "Here Document"? It is not really a form of "quoting" (though shell variable expansion may happen), since the text in a here document doesn't go onto a shell command line; the text is fed as standard input to the command. *) To turn off variable expansion inside a here document, put the terminator word on the command line in quotes: $ cat << 'EOF' This $SHELL will not expand. EOF This $SHELL will not expand. *) Skim over Section 8.3.12 (Shell Invocation Options). Important options: -c -u -v -x --noprofile *) (Section 8.3.13) How do you turn *on* a shell option from inside the shell? How do you turn *off* a shell option? Skim Table 8.50 - don't try to memorize it all. Other important options: noglob noclobber notify physical *) Skim over Section 8.3.14 (The shopt command). *) (Section 8.3.15) Start to become familiar with the names of these built-in commands. You should not create aliases or scripts that use these names; confusion will result. ---------------------------------------- Chapter 8 Lab Exercise Questions (p.380) ---------------------------------------- After a "first pass" reading of Chapter 8, you should be able to answer these question numbers: Lab 1: 1 3 5 7 14 Lab 2: 2 3 4 5 6 7 Lab 3: 1 12 Lab 4: 1 2 Lab 5: 1 2 3 4 5 7 8 Lab 6: 1 2 3 5