CHAPTER 6: THE SHELL
Enter the commands echo "$SHELL" and echo '$SHELL'. What difference do you notice?
$SHELL is evaluated in double quotes only.
Frame wild-card patterns (i) where the last character is not numeric, (ii) that have at least four characters.
(i) *[!0-9] (ii) ????*
How do you find out the number of (i) users logged in, (ii) directories in your home directory tree?
(i) who | wc -l (ii) find $HOME -type d -print | wc -l
Command substitution requires the command to use: (i) standard input, (ii) standard output, (iii) both, (iv) none of these.
(ii) Standard output
To append .c to a variable x, you have to use the expression (i) $x.c (ii) $x".c" (iii) ${x}.c (iv) any of these (v) only the first two.
(iv) any of these
Why does the shell need to expand wild cards? How does it treat the ** when used as an argument to a command (like echo **)?.
Because they mean nothing to the command. A * is expanded to match all filenames in the current directory.
Shells typically seen on Unix system?
Bourne shell Korn shell Bash shell (bourne again shell)
Which Shell is the default?
Bourne shell - /bin/sh
The Bourne shell family and its derivatives are?
Bourne shell - /bin/sh Korn shell - /bin/ksh Bash (Bourne-again shell) - /bin/bash
The C shell and its derivative are?
C shell - /bin/csh tcsh - /bin/tsch
Advantages of tcsh?
Command history Command line editing Auto-completion of file names and variables as well as programmable completion at the command line ****Alias argument selectors; the ability to define an alias to take arguments supplied to it and apply them to the commands that it refers to (Tcsh is the only shell that provides this feature)**** Wildcard matching Job control
C-Shell-/bin/csh
Created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s and distributed with BSD UNIX.
Bash (Born again shell) - /bin/bash
Created by Brian Fox for the GNU Project as a free software replacement for the Bourne shell and first released in 1989. It has been distributed widely as it is a default shell on the major Linux distributions and OS X.
TENEX c-shell - tcsh?
Created by Ken Greer at Carnegie Mellon University in December 1981. The "t" in tcsh comes from the "T" in TENEX, an operating system with a command-completion feature. Compatible with C shell
Korn Shell - /bin/ksh
Developed by David Korn at Bell Labs in the early 1980s. It is backward-compatible with the Bourne shell and includes many features of the C shell. ***NOT USED MUCH ANYMORE***
Bourne shell - /bin/sh
Developed by Stephen Bourne at Bell Labs and released in 1977 in the Version 7 Unix, it was a replacement for the Thompson shell.
Quoting?
Enclosing the wild card, or even the entire patter, within quotes (i.e., "chap*"). Anything within the quotes (baring few exceptions) is left alone by the shell and not interpreted.
What differentiated the C shell from others, especially in the 1980s?
Its interactive features and overall style. Its new features made it easier and faster to use. The overall style of the language looked more like C and was seen as more readable.
Major differences between KornShell and the traditional Bourne shell include:
Job control, command aliasing, and command history designed after the corresponding C shell features (Job control was added to the Bourne Shell in 1989) A choice of three command line editing styles based on vi, Emacs, and XEmacs Associative arrays and built-in floating point arithmetic operations Dynamic extensibility of built-in commands
Is the output of the command cat foo1 foo2 >/dev/tty directed to the standard output?
No, but to the file representing the terminal.
Attempt the variable assignment x = 10 (space on both sides of the =). Does it work if you are not using the C shell?
No, it doesn't. The shell interprets x as a command and = and 10 as its arguments.
What is tcsh?
On many systems, such as Mac OS X and Red Hat Linux, csh is actually tcsh, an improved version of csh.
The Shell's Treatment of the Command Line:
Parsing Variable evaluation Command substitution Redirection Wild-card interpretation PATH evaluation
How do you split a long command sequence into multiple lines?
Press a \ before pressing [Enter].
Escaping?
Providing a \ (backslash) before the wild card to remove (escape) its special meaning
Where to Use Shell Variables
Setting Pathnames $ progs='/home/romeo/c_progs' $ cd $ progs ; pwd /home/romeo/c_progs Using Command Substitution $ mydir=`pwd` ; echo $mydir /home/romeo/c_progs
the xargs command
Sometimes, the filenames used by commands can be determined only at runtime. UNIX provides a real dark horse—the xargs command—that can run any command but obtains the file list from standard input. Das, Sumitabha. Your UNIX/Linux: The Ultimate Guide, 3rd edition (Page 183). McGraw-Hill Higher Education -A. Kindle Edition.
What is the significance of the command ls *.*? Does it match filenames that begin with a dot?
The command lists all filenames where a dot occurs anywhere in the filename except at the beginning.
wild cards
The metacharacters used to match filenames belong to a category called? (something like the joker that can match any card).
shell scripts
The shell offers the facility of storing a group of commands in a file and then executing the file. All such files are called Das, Sumitabha. Your UNIX/Linux: The Ultimate Guide, 3rd edition (Page 181). McGraw-Hill Higher Education -A. Kindle Edition.
Name the three sources and destinations of standard input and standard output.
The terminal, file, and pipe.
environment variables.
The variables that control the workings of the UNIX system are known as Das, Sumitabha. Your UNIX/Linux: The Ultimate Guide, 3rd edition (Page 184). McGraw-Hill Higher Education -A. Kindle Edition.
The commands cat and wc, when used without arguments, don't seem to do anything. What does that indicate, and how do you return the shell prompt?
They expect input from standard input—the terminal by default. Press [Ctrl-d] to return to the shell.
What is the file /dev/null used for?
To prevent extraneous output and error messages from appearing on the terminal.
Devise a command that copies all files named chap01, chap02, chap03, and so forth through chap26 to the parent directory. Can a single wild-card pattern match them all?
Use cp chap0[1-9] chap1[0-9] chap2[0-6] .. in the Bourne shell. For other shells, you can use chap{0[1-9],1[0-9],2[0-6]} as a single wild-card pattern.
How do you create a filename containing just one space character? How can you "see" the space in the ls output?
Use echo >\ [Enter] where the \ is followed by a space. The sequence ls | od -bc shows octal 040 as the filename.
When will cd * work?
When there is only one file in the current directory and that file is also a directory.
Is this a legitimate command, and what does it appear to do? >foo <bar bc
YES, Legitimate; the bc command reads standard input from bar and writes standard output to foo.
Is the wild-card expression [3-h]* valid?
Yes, because 3 has a higher value than h in the ASCII collating sequence.
The >> metacharacter
can also be used to append
The < metacharacter
can reassign, or redirect, stdin
The > metacharacter
can reassign, or redirect, stdout and stderr
Match the filenames chapa, chapb, chapc, chapx, chapy, and chapz with a wild-card expression.
chap[a-cx-z]
What command do you invoke to know what shell you are using?
echo $SHELL
Each of the three standard files is represented by a number called a
file descriptor 0 = Standard input 1 = Standard output 2 = Standard error
Commands in the fourth category, using standard output and standard input are called? Das, Sumitabha. Your UNIX/Linux: The Ultimate Guide, 3rd edition (Page 184). McGraw-Hill Higher Education -A. Kindle Edition. are called ?
filters
Which UNIX command uses wild cards as part of its syntax?
find
tee
is an external command and not a feature of the shell **It duplicates its input, saves one copy in a file, and sends the other to the standard output** Example: $ who | tee user.txt romeo pts/2 Sep 7 08:41 (pc123.heavens.com) juliet pts/3 Sep 7 17:58 (pc122.heavens.com) sumit pts/5 Sep 7 18:01 (mercury.heavens.com) $ cat user.txt romeo pts/2 Sep 7 08:41 (pc123.heavens.com) juliet pts/3 Sep 7 17:58 (pc122.heavens.com) sumit pts/5 Sep 7 18:01 (mercury.heavens.com)
A variable assignment
is of the form variable=value (no spaces around the =), but its evaluation requires the $ as prefix to the variable name. **evaluated by prefixing a $ to the variable name** $ count=5 $ echo $count 5 A variable can also be assigned the value of another variable $ total=$count $ echo $total 5
The | (pipe) meta character:
is used to redirect the output of one command to the input of another command. ( enables a command to obtain its standard input from the standard output of another command) Example: Instead of this... $ who > user.txt $ wc -l < user.txt4 Use this... $ who | wc -l4
How do you save your entire home directory structure, including the hidden files, in a separate file?
ls -lRa $HOME > foo
How do you remove only the hidden files of your directory? Does rm * remove these files as well?
rm .[!.]* removes all files beginning with a dot except the . and .. directories. rm * doesn't remove these files.
command substitution
the shell enables one or more command arguments to be obtained from the standard output of another command (enables a command's standard output to become the arguments of another command.) Das, Sumitabha. Your UNIX/Linux: The Ultimate Guide, 3rd edition (Page 184). McGraw-Hill Higher Education -A. Kindle Edition.