 |
 |
webpointmorpheus Linux Info
The BASH and Other Shells
|
|
Introduction
Command I/O
BASH Filter Commands
Variables
Shell Scripting
BASH Builtins
SED Stream Editor
The awk Command
BASH Resources
Documents in this Series
©2005 - material compiled by Bob Carnaghi, www.webpointmorpheus.com
|
-
- Introduction Top of Page
- A Linux shell is a command line interpreter, the method of user interaction with the operating sytem. Everyting that happens in Linux between the user and the operating system must take place in a shell of some kind. There are several shells available, such as
BASH (BOURNE again shell), csh (C Shell), zsh (Z shell), KORN shell, and others. Each shell has its strong and weak points, and the BASH shell is the most popular. This document considers the BASH shell exclusively.
- Listed in the table immediately below are some of the most basic commands that a BASH shell user will find useful. If more information about any of the commands is necessary, type
man command to view the man page for the command. Addional system commands are listed further along in this document. For more information on Linux system interaction, see the resource section below.
-
| Basic BASH Functionality |
| Command |
Description |
clear |
Clears the terminal screen of previous commands and history. |
reset |
Resets the terminal to the default settings. |
finger |
Displays information about system users. Must enter the username. |
who |
Displays the users who are currently logged into the system. |
w |
Displays the users who are currently logged into the system along with their tasks. |
whoami |
Displays your login name. |
id |
Displays your UIDs, GID's. |
date |
Displays the current date and time. |
cal |
Displays the current month in calendar form. Note that the command is not limited to only the current month: enter another month and/or year for further details. |
exit |
Will exit the current shell. |
-
- Command Input & Output Top of Page
- Entering commands is what accomplishes tasks in the system, but what is to be done with the output of the commands? Command redirection allows the output of a command to be sent to a file and saved on the hard drive or elsewhere. The common redirection characters are listed in the table below. Another common method of moving output of commands around is to send the output of one command to another command using
pipes (|). There can be several pipes in one command line entry that moves the output of command one to the input of command two which moves the output of command two to the input of command three, etc. It's possible to finally redirect the output of the last command to a file or elsewhere.
- Command Input/Output (I/O) is approached from a threefold viewpoint: Standard Input (stdin), Standard Output (sdtout), and Standard Error (stderr). Each command that is entered has available all three streams of data, and the individual streams can be redirected. See the table below for more details.
-
| BASH Redirection Character Sequences |
| Command & Character Sequence |
Description |
command 1>file
command >file |
command STDOUT is redirected to a file (not to the terminal screen) |
command 2>file |
command STDERR is redirected to a file (not to the terminal screen) |
command 1>fileA 2>fileB
command >fileA 2>fileB |
command STDOUT is redirected to fileA (not to the terminal screen) & command STDERR is redirected to fileB (not to the terminal screen) |
command 1>file 2>&1
command >file 2>&1
command 1>&2 2>file
command >&2 2>file
|
both command STDOUT & STDERR are sent to the same file (not to the terminal screen) |
command 1>>file
command 1>>file |
command STDOUT is redirected and appended to fileA (not to the terminal screen) |
command 2>>file |
command STDERR is redirected and appended to fileA (not to the terminal screen) |
command 0<file
command <file |
command STDIN is taken from a file |
Notes:
- The default action of command redirection to a file is to create the file if it does not exist, and to overwrite the file if it does exist.
- When redirection is indicated on the command line, the BASH shell will clear the contents of the file. For command sequences that utilize complex redirection and pipes, this may cause unexpected results.
|
-
- BASH Filter Commands Top of Page
- Filter commands are designed to be used in a pipe sequence to alter the output from a command before moving it along to another command or printing it to the terminal screen. Typically, filter commands do not stand alone, as they depend on input from another command or file. Listed below are come of the more common filter commands.
-
| BASH Useful Filter Commands |
| Command (-flag) |
Description |
sort |
sorts the lines in a file by alphanumeric character |
sort -r |
reverse the output of the sort command |
wc |
(word count) - counts number of characters, words, or lines in a file |
wc -c |
(word count) - counts number of characters in a file |
wc -w |
(word count) - counts number of words in a file |
wc -l |
(word count) - counts number of lines in a file |
pr |
(print) - formats a file for printing, has many options |
pr -d |
(print) - formats a file for printing with double-spaces |
tr |
(translate) - command that will 'translate' certain characters in a file |
nl |
(number line) - numbers the lines in a file |
grep * |
searches the text within a file and returns the lines that match a regular expression |
awk * |
command that will return a formatted result from a text file based upon pattern matching and specific action statements |
sed * |
(stream editor) - command that can search and replace text in a file and return a formatted result |
Notes:
*This is an extremely useful and versatile BASH command. There are entire books as well as extensive online documentation about this command.
|
-
- Environment & User-Defined Variables Top of Page
- The BASH shell, indeed the Linux operating system itself, has as one of its attributes, the concept of
variables, typically associated with the shell (environment variables), or the user (user variables). There are additional system variables that are set by various methods and utilities. To see the shell variables, type the set command. Environment variables can be viewed by entering the command env at the command prompt of the shell. Individual variables can be viewed by typing echo $VARIABLE at the command prompt.
- Listed in the table below is a list of BASH shell variables. Note that all of these variables may not be set in any given shell environment, some may need to be configured per user login, or manually. For more information about these variables, type
man bash. For more information about setting, accessing, and using variables, type man set or man env.
-
| BASH SHELL Variables |
| Variable |
Description |
PPID |
The process ID of the shell's parent. |
PWD |
The current working directory as set by the cd command. |
OLDPWD |
The previous working directory as set by the cd command. |
REPLY |
The line of input read by the read builtin command when no arguments are supplied. |
UID |
The user ID of the current user. |
EUID |
The effective user ID of the current user. |
BASH |
The full pathname used to invoke the current instance of bash. |
BASH_VERSION |
The version number of the current instance of bash. |
SHLVL |
A counter that is incremented by one each time an instance of bash is started. |
RANDOM |
A random number integer generator. May be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even when reset. |
SECONDS |
The number of seconds since the current shell invocation. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even when reset. |
LINENO |
A decimal number representing the current sequential line number (starting with 1) within a script or function. When not in a script or function, the value substituted is not guaranteed to be meaningful. When in a function the value is an approximately the number of simple commands executed in the current function. If LINENO is unset, it loses its special properties, even when reset. |
HISTCMD |
The history number of the current command. If HISTCMD is unset, it loses its special properties, even if it is subsequently reset. |
OPTARG |
The value of the last option argument processed by the getopts builtin command. |
OPTIND |
The index of the next argument to be processed by the getopts builtin command. |
HOSTTYPE |
Set to a string that uniquely describes the type of machine on which bash is executing. The default is system-dependent. |
IFS |
The Internal Field Separator that is used for word splitting after expansion, as well as to split lines into words with the read builtin command. The default value is ``<space><tab><>newline>''. |
PATH |
A colon-separated list of directories in which the shell looks for commands. The default path is system-dependent, and is typically set during installation, or by the administrator who installs bash. |
HOME |
The home directory of the current user. |
CDPATH |
A colon-separated list of directories that are the search path for the cd command. |
MAIL |
If this parameter is set to a filename, and the MAILPATH variable is not set, bash informs the user of the arrival of mail in the specified file. |
MAILCHECK |
Specifies how often (in seconds) bash checks for mail. |
MAILPATH |
A colon-separated list of pathnames to be checked for mail. |
MAIL_WARNING |
A configurable mail read warning message. |
PS1 |
The primary prompt string used at the command line. |
PS2 |
The secondary prompt string used at the command line. |
PS3 |
The select prompt string used at the command line. |
PS4 |
The prompt string used at the command line during execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. |
HISTSIZE |
The number of commands to remember in the command history. The default value is 500. |
HISTFILE |
The name of the file in which command history is saved. The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits. |
HISTFILESIZE |
The maximum number of lines contained in the history file. |
PROMPT_COMMAND |
If set, the value is executed as a command prior to issuing each primary prompt. |
IGNOREEOF |
Controls the action of the shell on receipt of an EOF character as the sole input. |
TMOUT |
If set to a value greater than zero, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt. Bash terminates after waiting for that number of seconds if input does not arrive. |
FCEDIT |
The default editor for the fc builtin command. |
FIGNORE |
A colon-separated list of suffixes to ignore when performing filename completion. |
INPUTRC |
The filename for the readline startup file which will override the default of ~/.inputrc. |
HISTCONTROL |
Variable that controls what is entered into the HISTORY list, and how it is entered. |
command_oriented_history |
If set, bash attempts to save all lines of a multiple-line command in the same history entry. This allows easy re-editing of multi-line commands. |
glob_dot_filenames |
If set, bash includes filenames beginning with a `.' in the results of pathname expansion. |
allow_null_glob_expansion |
If set, bash allows pathname patterns which match no files to expand to a null string rather than themselves. |
histchars |
The two or three characters which control history expansion and tokenization. |
nolinks |
An option that can be set such that the shell does not follow symbolic links when executing commands that change the current working directory. It uses the physical directory structure instead. |
HOSTFILE |
Contains the name of a file in the same format as /etc/hosts that should be read when the shell needs to complete a hostname. The file may be changed interactively; the next time hostname completion is attempted bash adds the contents of the new file to the already existing database. |
noclobber |
If set, bash does not overwrite an existing file with the >, >&, and <> redirection operators. This variable may be overridden when creating output files by using the redirection operator >| instead of >. For more info, see the -C option to set. |
auto_resume |
This variable controls how the shell interacts with the user and job control. See the man page for the complexities of this variable. |
no_exit_on_failed_exec |
If this variable has been assigned, it renders a non-interactive shell such that it will not exit if it cannot execute the file specified in the exec builtin command. An interactive shell does not exit if exec fails. |
cdable_vars |
A variable that can be assigned as an argument to the cd builtin command. The assigned value is assumed to be a value that is the directory to change to. |
NOTES: For a list of all BASH options and their settings, type set -o.
|
-
- Shell Scripting Top of Page
- The bash shell is capable of execuing files that have been created and configured as scripts to complete admistrative and other tasks. These files typically contain a series of commands, functions, and other constructs that make it possible to do almost anything on the Linux system. The programming constructs included in the bash capability are, but are not limited to, if constructs, loops, case construct, and more. See the resources section below for more information.
- During script execution, interactive reading of input from the user is often necessary for proper command execution. The following escape sequences may be necessary to the
echo -e command.
-
| BASH SHELL Variables |
| Variable |
Description |
PPID |
The process ID of the shell's parent. |
\0xxx |
the character whose ASCII code is xxx in octal format |
\\ |
backslash |
\a |
alert (beep) |
\b |
backspace |
\c |
suppress trailing newline |
\f |
form feed |
\n |
new line |
\r |
carriage return |
\t |
horizontal tab |
\v |
vertical tab |
- BASH Builtin Commands Top of Page
- As a sophisticated and complex Linux utility, the BASH shell has within its capability several built-in commands. Listed below are most of these commands. Please note that the command options and many details are not included in this document. The BASH built-in commands are simply defined here for convenience, see
man bash for details.
-
| BASH Built-in Commands |
| Command |
Description |
alias |
Alias will create a substitute name for another command or script. Alias with no arguments prints the list of aliases in the form name=value on standard output. The Alias command has several options available. |
bg |
Place a specified job in the background, as if it had been started wit &. |
bind |
Display current readline key and function bindings, or bind a key sequence to a readline function or macro. |
break |
Exit from within a for, while, or until loop. |
builtin shell-builtin |
Execute the specified shell builtin, passing it arguments, and return its exit status. |
cd |
Change the current directory to the argument given, or to the user's home directory if no argument is given. |
command |
Run command with the arguments supplied, suppressing the normal shell function lookup. |
continue |
Resume the next iteration of the enclosing for, while, or until loop. |
declare & typeset |
Declare variables and/or give them attributes. |
dirs |
Display the list of currently remembered directories. Directories are added to the list with the pushd command; the popd command moves back up through the list. |
echo |
Output the arguments supplied, separated by spaces. |
enable |
Enable and disable builtin shell commands. |
eval |
The arguments supplied are read and concatenated together into a single command. |
exec |
If command is specified, it replaces the shell. No new process is created. |
exit |
Cause the shell to exit with a status of n. |
export |
The supplied arguments are marked for automatic export to the environment of subsequently executed commands. |
fc |
Fix Command. A range of commands from first to last is selected from the his-tory list. |
fg |
Place the specified job in the foreground, and make it the current job. |
getopts |
getopts is used by shell procedures to parse positional parameters. |
hash |
Supplies a list (hash table) of commands and number of times used (hits) during the current login session. |
help |
Display helpful information about builtin commands. |
history |
Displays the command history list with line numbers. |
jobs |
Lists the active jobs. |
kill |
Terminate processes. Note that there are levels of kill signals. |
let |
Permforms mathematical operations and assignment. |
local |
Create a local variable, and assign it value. |
logout |
Exit a login shell. |
popd |
Removes entries from the directory stack. |
pushd |
Adds a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the current working directory. |
pwd |
Print the absolute pathname of the current working directory. |
read |
Used for shell script interaction, prompts and reads user input. |
readonly |
The given arguments are marked readonly and the values of these names may not be changed by subsequent assignment. |
return |
Causes a function to exit with the return value specified by the argument given. |
set |
Displays, sets, or unsets shell attributes. |
shift |
Shifts command arguments. |
suspend |
Suspend the execution of this shell until it receives a SIGCONT signal. |
test |
Return a status of 0 (true) or 1 (false) depending on the evaluation of the conditional expression that is supplied as an argument. |
trap |
Expects a command as an argument, and the command is to be read and executed when the shell receives signal(s) sigspec. |
type |
Indicates how each supplied argument would be interpreted if used as a command name. type returns true if any of the arguments are found, false if none are found. |
ulimit |
Ulimit provides control over the resources available to the shell and to processes started by it on systems that allow such control. |
umask |
The user file-creation mask. Sets default file and directory permissions that are given to new entities when created. |
unalias |
Remove names from the list of defined aliases. |
unset |
Takes a name or list of names as an argument. For each name, remove the corresponding variable or, given the -f option, function. |
wait |
Wait for the specified process and return its termination status. |
-
- SED Stream Editor Top of Page
sed is a popular stream editor that works on a file line by line. sed acts like a filter by executing a group of editing instructions for the text file. sed is the program of choice for making changes to a text file. For a sed tutorial or book, see the resources section below.
-
List of Useful sed commands. |
| Command & Character Sequence |
Number of Arguments |
Description |
a |
1 |
append text |
blabel |
2 |
branch to label |
c |
2 |
change lines |
d |
2 |
delete lines |
g |
2 |
get contents of hold area |
h |
2 |
hold pattern space (in a hold buffer) |
i |
1 |
insert lines |
l |
2 |
list lines |
n |
2 |
next line |
p |
2 |
print |
q |
1 |
quit |
r file |
1 |
read the contents of file |
s/RE/replacement/[flags] |
2 |
substition |
tlabel |
2 |
test substitutions and branch if successful |
w file |
2 |
write to file |
x |
2 |
exchange buffer space with pattern space |
y/list1/list2/ |
2 |
translates list1 into list2 |
D |
2 |
delete first part of the pattern space |
G |
2 |
append contents of hold area |
H |
2 |
append pattern space on buffer |
N |
2 |
append next line |
P |
2 |
print first part of the pattern space |
!cmd |
2 |
taken to mean "Don't apply to specified addresses" |
# |
0 |
comment |
:label |
0 |
place a label |
= |
1 |
display line number |
{ |
2 |
group commands |
-
- The awk Programming Language Top of Page
awk is a programming language that can be applied to data-manipulation and computing tasks on a Linux operating system. For more information on awk, consult the resources immediately below.
- BASH Resources Top of Page
-
|
Other Documents in this Series
Top of Page
- Introduction and History
- Installation, Advanced Installation, and Usage
- The Linux Kernel and the Boot Process
- Filesystems - Management & Administration
- The BASH and Other Shells
- System Initialization and the X Environment
- Linux Processes
- Linux Administration, Peripherals, and Hardware
- Software Installation and Management
- Backups and Log Files
- Performance and Problems
- Network Configuration
- Security
- Key Linux Commands
- Essential Linux Definitions
|
|
webpointmorpheus Home
Technical Pages
|
Site Map
This page was last modified: Tuesday July 15, 2008 8:57 PM |
|
 |