==================== The Unix/Linux Shell ==================== -Ian! D. Allen - idallen@idallen.ca - www.idallen.com Some basic Shell concepts. Contents: * What is a shell for? * Most (but not all) commands take what as arguments? * How does the shell help run commands? * What is a "Bourne" shell? What is a "C" shell? * Basic Command Syntax * Shell interactive pathname completion * Shell "history" of previous commands * Shell command line aliases * Shell GLOB (wildcard) patterns * Command line order of processing * Shell I/O Redirection * Shell search PATH -------------------- What is a shell for? -------------------- To find and run programs. ("Programs" are also called commands or utilities.) Shells also do programming kinds of things; but, that programming is usually to aid in the finding and running of programs, not to do any kind of substantial mathematical or business calculations. Part of running a program involves supplying command line arguments to that program; the shell helps with that, too. --------------------------------------------------- Most (but not all) commands take what as arguments? --------------------------------------------------- Most - but not all - Unix commands take pathnames as arguments. Much of what people do online is manipulate files and directories. "Pathnames" are names that might be file names or directory names. (Unix also has names that are neither files nor directories, e.g. the /dev/null pathname is a "character special" device.) The shell has GLOB (wildcard) features to make matching pathnames easier. ------------------------------------- How does the shell help run commands? ------------------------------------- Command names are almost always the names of executable files. (Some command names are built-in to each shell.) Shells look for command name executable files in various places, using a list of directories stored in the $PATH environment variable. Shells provide aliases and variables to save typing the same things (commands or pathnames) over and over. Shells provide a "history" mechanism to recall and edit the last commands you enter, to save retyping them. Shells provide ways of completing command and file names, to save typing. Shells provide wildcards (GLOB patterns) to generate large or small lists of pathnames as arguments for commands. ----------------------------------------------- What is a "Bourne" shell? What is a "C" shell? ----------------------------------------------- The shells sh, ksh, zsh, and bash (the "Bourne" shells) all have a common ancestry. They are all derived from the original shell "sh" written in the 1970's by Stephen Bourne. The programming features of these shells (if statements, for loops, etc.) all look and work the same way. This is the best shell to study. The shells csh and tcsh (the "C" shells) are similar, having a history dating back to Bill Joy at Berkeley in the 1980's. Their syntax for programming is not the same as the Bourne shells. We do not cover the C shell syntax in this course; these shells are notoriously buggy. -------------------- Basic Command Syntax -------------------- Many Unix commands need both a VERB (what to do) and an OBJECT (what to do it on): $ cat /etc/passwd $ mail idallen@idallen.ca $ kill 1234 The following incorrect attempts at Unix commands are wrong: $ /etc/passwd (missing VERB; what are you trying to DO?) $ cat (missing OBJECT; catenate WHAT file?) Remember to tell Unix both what you want to do and to what object you wish to do it: $ less /etc/passwd $ cat /etc/passwd ------------------------------------- Shell interactive pathname completion ------------------------------------- If you type part of a pathname on a comand line and then push the TAB key (once or twice), many shells will attempt to complete the pathname for you. If the pathname cannot be completed unambiguously, the shell will show you a list of possible completions: bash$ echo /etc/pas passwd passwd- passwd.OLD If you type part of a command name and push TAB, the shell will list all the possible commands in your PATH that start with those letters: bash$ mkd mkdep mkdict mkdir mkdirhier mkdosfs ------------------------------------ Shell "history" of previous commands ------------------------------------ Most shells keep a record ("history") of the commands you type. The history is often saved in a file and restored when you next log in. You can see the history list using the built-in "history" command. Many shells allow you to use the UP-ARROW and DOWN-ARROW keys to move up and down in the command history. (Some shells use other key sequences, such as ^P and ^N.) Some shells let you select items from the history list by number (e.g. "!123"). See your shell's man page. -------------------------- Shell command line aliases -------------------------- Watch out for "helpful" system admin that define aliases for your shells when you log in. (This is true on most versions of Linux.) The aliases may mislead you about how Unix commands actually work. (For example, the "rm" command does *not* prompt you for confirmation. On some systems, when you log in, "rm" is made to be an alias for "rm -i", which *does* prompt.) To avoid pre-defined aliases, sometimes you can start up a fresh copy of the shell that has no aliases defined: $ alias [...many aliases may print here...] $ bash bash$ alias [...no more aliases here...] The other thing you can do is execute "unalias -a" to remove all your aliases for the current shell. You can put this into your shell start-up file (e.g. .bashrc) to do it every time you start a new shell. To define your own aliases, look up "aliases" in a Linux text index, e.g.: $ alias dir='ls -lF' $ alias cd..='cd ..' $ alias mali=mail You must put your own alias definitions in a shell start-up file to have them saved between sessions (e.g. put them into your .bashrc file). ------------------------------ Shell GLOB (wildcard) patterns ------------------------------ The shell will treat words on the command line containing GLOB characters as pathnames and try to match the patterns against pathnames to produce a list of names. See Class Notes file: glob_patterns.txt -------------------------------- Command line order of processing -------------------------------- The shell detects and expands various shell metacharacters in a strict order. See Class Notes file: order_of_processing.txt --------------------- Shell I/O Redirection --------------------- The shell can create or append the output of commands to files. It can take the output of one or more commands and send it as input to another command. See Class Notes file: redirection.txt ----------------- Shell search PATH ----------------- The shell searches for commands by name in a list of directories. See Class Notes file: search_path.txt -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/