|Front | Info | Lists | Newsfeeds | Study Guide | What is BSD?|
This is the BSDA Study Guide Book written via a wiki collaboration. This is a work in progress. You may contribute to or discuss this specific page at http://bsdwiki.reedmedia.net/wiki/Demonstrate_familiarity_with_the_default_shell.html.
Demonstrate familiarity with the default shell
In BSD systems, the "shell" is frequently discussed, often abused, sometimes cursed, and, on occasion, used by humans to perform work. For many users, and all system administrators, it's imperative to have knowledge of the "shell" as both an interactive command interpreter and a programming language in its own right.
Usually, a "shell" acts as a "system command interpreter" - a program which accepts input (in the form of "textual" commands and data), communicates with the machine to perform an action or otherwise process the input, and then communicates a result to an output device (usually the terminal/screen that you are viewing). This is known as using the shell "interactively". You can, however, use a shell as a seperate program (e.g., call a seperate "child instance" to perform some work); and most modern shells are also capable of performing multiple tasks simultaneously in interactive mode (see section Use Job Control for more details on this).
Generally the interactive shell produces a "prompt" at the bottom of your terminal for you to enter commands, and directs its output to the terminal as well. Of course, all this is rather customizeable (which is why this section is likely to be a rather long one).
In addition, you can create "scripts" using shell commands in sequence to perform complex sets of actions and perform logical operations. This is known as "shell scripting" (or shell programming) and is addressed in the section Create a simple Bourne shell script.
What Shell(s) Do I Get?
There are many, many shells available for use on computer systems; the ones that come with a default *BSD installation are "traditional" (that is, they are the same as or a derivative of a shell used on earlier releases). Here's a list of relevant shells for BSD systems past (the first two) and present (the rest):
If you are coming to BSD from a "GNU/Linux" background, you may notice the absence of "bash". Bash is available as a third party package, but isn't a traditional BSD shell, so it isn't available in a "default" install of Free-, Net-, or OpenBSD. If you *must have bash, see foo bar packages
A powerful feature of modern shells!
NOCLOBBER: keeping data safe
Using output redirection at the shell prompt you can redirect standard output to a file; for example, "echo $SHELL > myfile". You can also append data to the end of a file: "echo 'foo bar' >> myfile". But this can be dangerous; what if "mysh" already exists in the current directory and contains valuable data? And what if you meant to append (">>"), but accidentally only put in one ">"? Bye-bye "myfile"!
Fortunately in modern shells you can (and perhaps should) set "noclobber":
With "noclobber" set, files are protected from accidental replacement by the shell; you can use ">|" when you're sure you meant to overwrite the file!
At times it would be handy to have a short key combination for a long command, wouldn't it? That's where aliasing comes in handy. Aliases are set in shell resource files or at the prompt itself. In some BSD's, a number of "prebuilt" aliases are already present, particularly in ".cshrc" for the C shell (for example, "ll" is aliased to "ls -l" in FreeBSD's .cshrc).
Aliases are assigned using the keyword "alias"; if using sh or its variants, an equals sign ("="), and then an alias string. Currently assigned aliases are viewed by typing "alias" alone; an alias can be removed with "unalias aliasname".
See the Examples section for more details.
Bypassing a Command Alias
An alias may be "bypassed" on the command-line by preceding it with a "backslash" (). This is useful if you happen to have an alias with the same name as an actual program on the system. See the Examples for more details.
Selecting the user's default login shell is covered in section Change a user's default shell.
TODO: NOTES: OpenBSD, root, sh (which is really "ksh" which is really "pdksh"); sh, ksh, and csh on OpenBSD are all statically linked and therfore available in singleuser mode; FreeBSD, root, sh; users, tcsh which is tcsh and csh is also tcsh; NetBSD has old BSD sh, it's own "sh" (which others have forked to "ash") and pdksh.
! reverses the exit status of a pipeline (so it's the logical NOT of the result). If the result is 1 then it becomes 0, if the result is 0 it becomes 1.
also a shell pattern tool. *History substitutions begin with the character "!"
!! the previous event (equivalent to -1)
$ - the last argument
0 the first (command) word
h remove a trailing pathname component, leaving the head
t remove all leading pathname components, leaving the tail
r remove a filename extension ".xxx", leaving the root name
p named pipe (fifo)
[X] \ - temporarily bypass an alias \rm
escape a character, continue a command onto another line
modify shell behavior: temporarily permanently
prevent the shell from clobbering existing files:
use history substitution:
scroll the up key or down key to find appropriate command line
set command aliases to save time:
temporarily bypass a command alias:
executes the specified built-in command, cmd. This is useful when the user wishes to override a shell function with the same name as a built-in command.
sh(1), csh(1), and tcsh(1) including: !, !!, \$, 0, h, t, r, p, \