Demonstrate familiarity with the default shell

Author: KevinDKinsey

Reviewer: name contact BSD flavour

Reviewer: name contact BSD flavour


Concept

  • Be comfortable using the sh(1), csh(1) or tcsh(1) shells.
  • Be able to modify shell behavior both temporarily and permanently including:

    • prevent the shell from clobbering existing files
    • use history(((history))) substitution, and
    • set command aliases to save time at the command line.
  • Know how to temporarily bypass a command alias(((alias, command))).

Introduction

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 text 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 separate program (e.g., call a separate "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):

  • The Bourne Shell, written by Stephen Bourne of AT&T Labs for UNIX System 7 (1977).
  • The C Shell, written by Bill Joy for 2BSD in the late 1970s. It featured several "advances" over the Bourne Shell, including job control, history substitution, and aliases.
  • The TC Shell ("Tenex C Shell") originally by Ken Greer, an improved "C Shell". FreeBSD has "done away" with the "C Shell" in favor of tcsh, (e.g. /bin/csh == /bin/tcsh), and "tcsh" is the default shell for "normal" FreeBSD users.
  • The Korn Shell, originally by AT&T Labs staff; a "compatible upgrade" of the Bourne Shell - a derivative, the Public Domain Korn Shell (pdksh) is used as both a normal user and root's shell in OpenBSD. (In fact, /bin/sh on OpenBSD is really pdksh).
  • The Almquist Shell, or "ash", which was originally a Bourne shell replacement by Kenneth Almquist; derivatives of "ash" are actually "sh" (root's shell) on NetBSD and FreeBSD.

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 Sections Understand the difference between a pre-compiled binary and compiling from source and Understand when it is preferable to install a pre-compiled binary and how to do so to learn about installing software.

History Substitution

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":

""$ cat foo testing 1 2 3

""$ set noclobber

""$ echo "4 5 6" > foo foo: File exists.

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!

Command aliases

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.

Notes

! 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:

""sh -C ""sh -o noclobber (prevents command > file1 from overwriting file1 if it already exists) ""command >| file1 overrides the noclobber designation

use history substitution:

scroll the up key or down key to find appropriate command line

set command aliases to save time:

""alias name=[=string ...]

temporarily bypass a command alias:

""builtin cmd [arg ...]

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.

Examples

Practice Exercises

More information

sh(1), csh(1), and tcsh(1) including: !, !!, \$, 0, h, t, r, p, \