68 Bash Shell Reference
68 Bash Shell Reference
Table of Contents
COMMAND EXECUTION.....................................................................................................2
FILENAME SUBSTITUTION........................................................................................................................3
VARIABLES...........................................................................................................................3
VARIABLE SUBSTITUTION..................................................................................................3
SPECIAL PARAMETERS......................................................................................................4
SPECIAL VARIABLES...........................................................................................................4
JOB CONTROL.....................................................................................................................5
QUOTING..............................................................................................................................6
OPTIONS..............................................................................................................................6
Enabling/Disabling Options...........................................................................................................................6
CONDITIONAL EXPRESSIONS............................................................................................7
CONDITIONAL CONTROL COMMANDS...........................................................................10
BUILTIN COMMANDS........................................................................................................13
RESTRICTED SHELL........................................................................................................14
DEBUGGING BOURNE SHELL SCRIPTS.......................................................................14
FUNCTIONS........................................................................................................................15
ALIASES..............................................................................................................................15
COMMAND SEARCH PRIORITY......................................................................................16
FILES...................................................................................................................................16
SET and UNSET commands...............................................................................................17
REGULAR EXPRESSIONS.................................................................................................17
echo COMMAND:................................................................................................................18
PROMPT MANIPULATION.................................................................................................18
Job control (disown) Exercise: ...........................................................................................19
Bash session recording:......................................................................................................19
Monitoring a bash session from one or more users:...........................................................19
Bash options:.......................................................................................................................20
Command History and command line editing:.....................................................................21
EXAMPLE COMMANDS......................................................................................................22
68_Bash_Shell_Reference.sxw - 1
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
COMMAND EXECUTION
The primary prompt ($PS1 - default $ or # for super-users) is displayed whenever the
Bourne shell is ready to read a command.
The secondary prompt ($PS2 - default >) is displayed when the command is incomplete.
Command Execution Format
command1 ; command2 execute command1 followed by command2
command & execute command asynchronously in the background
command1 | command2 pass the standard output of command1 to standard input of
command2
command1 && command2 execute command2 if and only if command1 returns zero
(successful) exit status
eg. /sbin/lsmod | grep -q ^ipw2200 && { rmmod ipw2200; modprobe ipw2200; }
command1 || command2 execute command2 if and only if command1 returns non-zero
(unsuccessful) exit status
command \ continue command onto the next line. '\' must be the last char.
if { command ; } execute command in the current shell.
eg. if { cat /etc/motd &>/dev/null ; } ; then
if (command) execute command in a subshell.
eg. if (cat /etc/motd &>/dev/null) ; then
REDIRECTING INPUT/OUTPUT
The Bourne shell provides a number of operators that can be used to manipulate
command input/output, and files.
I/O Redirection Operators
<file redirect standard input from file
>file redirect standard output to file. Create file if non-existent, else overwrite.
>>file append standard output to file; Create file if non-existent.
<&- close standard input
>&- close standard output
<&n redirect standard input from file descriptor n
>&n redirect standard output to file descriptor n
n<file redirect file descriptor n from file
&>file redirect file both stdout(1) and stderr(2) file descriptors to file
n>file redirect file descriptor n to file. Create file if non-existent, else overwrite.
n>>file redirect file descriptor n to file. Create file if non-existent.
n<&m redirect file descriptor n from file descriptor m
n>&m redirect file descriptor n to file descriptor m
n<<x redirect to file descriptor n until x is read
n<<-x same as n<<x, except ignore leading tabs
n<&- close file descriptor n for standard input
n>&- close file descriptor n for standard output
&>n redirect standard output and standard error to file descriptor n
68_Bash_Shell_Reference.sxw - 2
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
FILENAME SUBSTITUTION
File name substitution is a feature which allows special characters and patterns to substituted with
file names in the current directory, or arguments to the case and test commands.
Pattern-Matching Characters/Patterns
? match any single character
* match zero or more characters, including null
[abc] match any characters between the brackets
[x-z] match any characters in the range x to z
[a-ce-g] match any characters in the range a to c or e to g
[!abc] match any characters not between the brackets
[!x-z] match any characters not in the range x to z
. strings starting with . must be explicitly matched
VARIABLES
Variables are used by the Bourne shell to store values. Variable names can begin with an
alphabetic or underscore character, followed by one or more alphanumeric or underscore
characters. Other variable names that contain only digits or special characters are reserved for
special variables (called parameters) set directly by the Bourne shell.
Variable Assignment Format
variable=, variable="" declare variable and set it to null
variable=value assign value to variable
variable=value command Set the variable with value and run the command
VARIABLE SUBSTITUTION
Variable values can be accessed and manipulated using variable expansion. Basic expansion is
done by preceding the variable name with the $ character. Other types of expansion use default
or alternate values, assign default or alternate values, and more.
Variable Expansion Format
$variable value of variable
${variable} value of variable
${#variable} numeric length(number of chars.) of value of variable
${variable:-word} value of variable if set and not null, else print word .
If : is omitted, variable is only checked if it is set.
eg. echo ${USER:-halo} ; echo $USER
${variable:+word} value of word if variable is set and not null, else nothing is
substituted. If : is omitted, variable is only checked if it is set.
eg. echo ${USER:+halo} ; echo $USER
${variable:=word} value of variable if set and not null, else variable is set to word,
then expanded. If : is omitted, variable is only checked if it is set.
${variable:?} value of variable if set and not null, else print "variable: parameter
null or not set". If : is omitted, variable is only checked if it is set.
${variable:?word} value of variable if set and not null, else print value of word and
exit. If : is omitted, variable is only checked if it is set.
68_Bash_Shell_Reference.sxw - 3
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
SPECIAL PARAMETERS
Some special parameters are automatically set by the Bourne shell, and usually cannot be
directly set or modified. The $n can be modified by the command set aaa bbb ccc ...
$n Positional parameter n max. n=9 ($0 is the name the shell script)
${nn} Positional parameter nn (for nn>9)
$# Number of positional parameters (not including the script program)
$@, $* All positional parameters
"$@" Same as "$1" "$2" . . . "$n"
"$*" Same as "$1c$2c . . . $n" c = content of $IFS (default is space)
$? Exit status of the last command
$$ Process ID of the current shell
$- Current options in effect
$! Process ID of the last background command
$is Name of the curent shell (in this case 'bash')
The shift command:
The command shift moves the assignment of the positional parameters to the left.
eg. script1 aaa bbb ccc ddd
(inside the script script1) ($1 $2 $3)
echo $1 $2 $3 -------> result aaa bbb ccc
SPECIAL VARIABLES
There are a number of variables provided by the Bourne shell that allow you to customize
your working environment. Some are automatically set by the shell, some have a default
value if not set, while others have no value unless specifically set.
Special Variables (keywords)
CDPATH search path for cd when not given a full pathname;
multiple pathnames are separated with a colon (no default)
HOME default argument for the cd command; contains the path of home directory
IFS internal field separator (default is space, tab, or newline)
LANG contains the name of the current locale
MAIL name of mail file to use if MAILPATH not set
MAILCHECK specifies how often to check for mail in $MAIL or $MAILPATH.
If set to 0, mail is checked before each prompt. (default 600 seconds)
MAILPATH contains a list of colon-separated file names that are checked for mail. File
names can be followed by a "%" and a message to display each time new
mail is received in the mail file. (no default)
PATH search path for commands; multiple pathnames are separated with a colon
(default /bin:/usr/bin:)
PS1 primary prompt string (default: $, #)
PS2 secondary prompt string (default: '>')
68_Bash_Shell_Reference.sxw - 4
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
SHACCT Contains the name of the accounting file that contains accounting records
for user shell procedures.
SHELL Pathname of the shell
TERM Specifies your terminal type: xterm=in X-window environment
screen='konsole' in 'screen' mode linux=from tty virtual terminal
dumb=system(eg. shell scripts executed from cron)
JOB CONTROL
Job control is a process manipulation feature found in the Bourne shell when invoked as
jsh. It allows programs to be stopped and restarted, moved between the foreground and
background, their processing status to be displayed, and more. When a program is run in
the background, a job number and process id are returned.
Job Control Commands
bg [%n] Resume current or stopped job n in the background
fg [%n] Move current or background job n into foreground
jobs [option] Display status of all jobs
-n Status since last job change
-r List of running jobs only
-s List stopped jobs only
-l display status of all jobs and their process ID's
-p display process ID's of all jobs
jobs -x command Replace job n in command with corresponding process group
id, then execute command
kill [-signal] %n Send specified signal to job n (default 15)
stop %n Stop job n
stty [-]tostop Allow/prevent background jobs from generating output
suspend Suspend execution of current shell
wait Wait for all background jobs to complete
wait %n Wait for background job n to complete
Ctl-z Stop current job
disown [option] [%n] Disown the last activated(+) background job or job %n.
A disowned job will not die when shell dies. init will be its father.
-a Disown all the background jobs
-r Disown only the running jobs
-h Disown active job (+)from shell only when shell is closed:
Job Name Format
%%, %+ current job
%n job n
%- previous job
%string job whose name begins with string
%?string job that matches part or all of string
68_Bash_Shell_Reference.sxw - 5
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
QUOTING
Quotes are used when assigning values containing whitespace or special characters, to
delimit variables, and to assign command output. They also improve readability by
separating arguments from commands.
'...' remove the special meaning of enclosed characters except '
"..." remove the special meaning of enclosed characters except $, ', and \
\c remove the special meaning of character c
`command` replace with the standard output of command . Same as $(command)
Meta- In the open: $ & ; ( ) { } [ ] * ? ! < > \
characters in
In Double Quotes " ": $ ! \
bash:
OPTIONS
The Bourne shell has a number of options that specify your environment and control
execution. They can be enabled/disabled with the set command or on the sh or jsh
command line. Some options are only available on invocation.
Enabling/Disabling Options
List of Options
-a automatically export variables that are defined
-c commands read and execute commands (w/sh only)
-e exit if a command fails
-f disable file name expansion
-h remember locations of functions on definition instead of on execution
(see also hash)
-i execute in interactive mode (w/sh only)
-k put variable assignment arguments in environment
-n read commands without executing them
-p do not set effective ids to real ids
-r run a restricted shell (w/sh only)
-s read commands from standard input (w/sh only)
-t exit after reading and executing one command
-u return error on substitution of unset variables
-v display input lines as they are read
-x display commands and arguments as executed
68_Bash_Shell_Reference.sxw - 6
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
CONDITIONAL EXPRESSIONS
The test and [...] commands are used to evaluate conditional expressions with file
attributes, strings, and integers. The basic format is:
test expression
or
[ expression ]
Where expression is the condition you are evaluating. There must be whitespace after the
opening bracket, and before the closing bracket. Whitespace must also separate the
expression arguments and operators. If the expression evaluates to true, then a zero exit
status is returned, otherwise the expression evaluates to false and a non-zero exit status is
returned.
Test File Operators
-a file True if file exists.
-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-g file True if file exists and is set-group-id.
-h file True if file exists and is a symbolic link.
-k file True if file exists and its ``sticky'' bit is set.
-p file True if file exists and is a named pipe (FIFO).
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than zero.
-t fd True if file descriptor fd is open and refers to a terminal.
-u file True if file exists and its SUID bit is set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-O file True if file exists and is owned by the effective UID.
-G file True if file exists and is owned by the effective GID.
-L file True if file exists and is a symbolic link.
-S file True if file exists and is a socket.
-N file True if file exists and has been modified since it was last read.
file1 -nt file2 True if file1 is newer (according to modification date) than
file2, or if file1 exists and file2 does not.
file1 -ot file2 True if file1 is older than file2, or if file2 exists and file1
does not.
68_Bash_Shell_Reference.sxw - 7
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
file1 -ef file2 True if file1 and file2 refer to the same device and inode
numbers.
-o optname True if shell option optname is enabled.
See the list of options under the description of the -o option to the
set builtin below.
Test String Operators
-n string True if length of string is not zero
-z string True if length of string is zero
string True if string is not set to null
string1 = string2 True if string1 is equal to string2
string1 == string2 “ “ “ “ “ “ “ “
string1 != string2 True if string1 is not equal to string2
string1 < string2 True if string1 sorts before string2 lexicographically in
the current locale.
string1 > string2 True if string1 sorts after string2 lexicographically in the
current locale.
string = pattern True if string matches pattern
string != pattern True if string does not match pattern
68_Bash_Shell_Reference.sxw - 8
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
68_Bash_Shell_Reference.sxw - 9
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
------for-------
for variable in word1 word2 . . . wordn
do
commands
done
Executes commands once for each word, setting variable to successive words each
time.
---------------------------------------------------------
for ((var=; $var<=limitvalue; var++))
do
commands
done
Executes commands for each loop where var is an integer variable which is set initially
with initialvalue, is incremented of '1' at each loop(var++) and will keep looping until
var has exceeded the limitvalue.
eg1. for ((i=100; $i>=10; i=i-5)) (from 100 to 10(included) step -5)
eg.2 for ((i=1; $i<=10; i++))
do
echo "Value of \$i is $i"
done
Loops 10 times. For the initial loop the nalues of $i is '1'. At each subsequent loop the
value of $i is incremented. The loop is not any more executed when the value of $i is
higher than 10.
for variable
do
commands
done
Execute commands once for each positional parameter, setting variable to successive
positional parameters each time.
---------------------------------------------------------
------until------
until command1 or
until test
do
commands
done
Execute commands until command1 returns a zero exit status
---------------------------------------------------------
------while------
while command1 or
while test
do
commands
done
Execute commands while command1 returns a zero exit status.
68_Bash_Shell_Reference.sxw - 10
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
or
------if------
if command1 or
if (command1) or
if { command1 ; } ;then
commands
fi
Execute commands if command1 returns a zero exit status.
Command in (...) are executed in a forked shell, commands in { ... ; } are
executed in the same shell. The ; at the end of commands, the spaces between { } and
the commands are important.
---------------------------------------------------------
if test_expression ; then
commands
fi
Execute commands if test_expression is true (returns a zero exit status).
test_expression is in format test expression or is enclosed in [ expression ].
It uses the format listed in page 8 & 9.
---------------------------------------------------------
if command1 ; then
commands2
else
commands3
fi
Execute commands2 if commands1 returns a zero exit status, otherwise execute
commands3.
---------------------------------------------------------
if command1
then
commands
elif command2 ; then
commands
. . .
elif commandn ; then
68_Bash_Shell_Reference.sxw - 11
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
commands
else
commands
fi
Ifcommand1 returns a zero exit status, or command2 returns a zero exit status, or
commandn returns a zero exit status, then execute the commands corresponding to the
if/elif that returned a zero exit status. Otherwise, if all the if/elif commands return
a non-zero exit status, execute the commands between else and fi.
Extra if examples:
Execute commandsx associated with the pattern that matches value; patterns can
contain the special filename substitution characters like *, ?, and [ ]. Multiple patterns
can be given but must be separated with a '|' character.
---------------------------------------------------------
--------Interrupting Loops------
for, while, or until loops can be interrupted by break or continue commands.
break command transfers the control to the command after the done command,
terminating the execution of the loop.
continue command transfers control to the done command, which continues execution
of the loop.
68_Bash_Shell_Reference.sxw - 12
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
BUILTIN COMMANDS
68_Bash_Shell_Reference.sxw - 13
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
RESTRICTED SHELL
Running the restricted shell rsh is equivalent to sh, except the following are not allowed:
- changing directories
- setting the value of PATH or specifying the path of a command
- running command of which their names contain one or more '/'
- and redirecting output with '>' or '>>'.
68_Bash_Shell_Reference.sxw - 14
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
FUNCTIONS
- They are normally used like fast local mini-scripts within a shell which need to be
called more than once within the interactive shell or script.
- Variables can be passed-on to functions and will be recognized as $1 $2 $3 etc.
In fact the following variables are local within a function:
$1 - $9 Positional parameters
$# Number of positional parameters
$* "$1 $2 $3 ..."
$@ "$1" "$2" "$3" ...
- The Positional parameter $0 and all other variables stay global within the shell
unless the command local VariableName is given within the function.
Within a function, the variable FUNCNAME is used instead of the $0.
- Global shell or exported variables can be changed within the function.
- Functions do not return variables except for the return number,
eg. return 5. return command will also terminate the function immediately.
The return number can then be read as a normal exit code using the $?.
- In scripts normally functions are included at the top so that they are read in first.
- Environment functions can be put into a file and read in with the '.' command.
- Functions may be recursive. No limit is imposed on the number of recursive calls.
- Functions can be exported, using the command: export -f FunctionName
- Function syntax:
FunctionName() { or function FunctionName {
commands ; commands ;
} }
- The command: unset -f FunctionName Deletes an existing function.
ALIASES
- Aliases are normally used to create command shortcuts(short names).
- Aliases are NOT exportable: not passed-on to sub-shells or child process.
- Aliases are not recognized in scripts.
- An alias can call another alias within the command.
eg. alias li="ls -l"; alias al="li -a" : al calls the alias 'li'
- Parameters added to alias will be added at the end of the real command.
- The parameters variables ($1, $2, $3 ...etc.) cannot be used within aliasses.
- Aliases are often defined in a file run within a script
(eg. ~/.bashrc or ~/.profile) with the dot '.' command.
- Alias commands:
alias AliasName="command(s)..." Sets a new alias value
eg. alias cp="cp -i"replaces the original command cp with cp -i for
interactive copying.(asks before overwriting files)
unalias AliasName Un-sets(deletes) the alias.
alias Displays all the current shell aliases.
68_Bash_Shell_Reference.sxw - 15
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
FILES
68_Bash_Shell_Reference.sxw - 16
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
set
Syntax: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
The set command is used to:
- Set bash operating attributes(using options)
- To assign values to positional parameters: eg.
set -a Automatically mark variables and functions which are modified
or created for export to the environment of subsequent commands.
set aaa bbb ccc
Assigns the value aaa to $1, bbb to $2 and ccc to $3.
unset
Syntax: unset [-fv] [name ...]
For each name, remove the corresponding variable or function.
Each unset variable or function is removed from the environment passed to
subsequent commands. If any of RANDOM, SECONDS, LINENO, HISTCMD,
FUNCNAME, GROUPS, DIRSTACK are unset, they lose their special
properties, even if they are subsequently reset.
The exit status is true unless a name does not exist or is readonly.
-v If no options are supplied, or the -v option is given, each name refers to a
shell variable.
Read-only variables may not be unset.
-f Each name refers to a shell function, and the function definition is removed.
eg. unset DISPLAY : Deletes the variable DISPLAY
unset -f startx : Deletes the function startx
REGULAR EXPRESSIONS
c non-special character c
\c special character c
^ beginning of line
$ end of line
. any single character
[abc] any character a, b, or c
[a-c] any character in range a through c
[^abc] any character except a, b, or c
[^a-c] any character except characters in a-c
\n nth \(...\) match (grep only)
rexp* zero or more occurrences of rexp
rexp+ one or more occurrences of rexp
rexp? zero or one occurrence of rexp
rexp1 |
regular expressions rexp1 or rexp2
rexp2
\(rexp\) tagged regular expression rexp (grep)
(rexp) regular expression rexp (egrep)
68_Bash_Shell_Reference.sxw - 17
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
echo COMMAND:
echo -e "...\a..." Alert (bell) --Note: only in Virtual Terminal(not in xterm)
"" "" "...\b..." Backspace
"" "" "...\c..." Suppress trailing new line
"" "" "...\f..." Form Feed
"" "" "...\n..." New Line echo -e "\012"
"" "" "...\r..." Carriage Return
"" "" "...\t..." Horizontal Tab echo -e "\011"
"" "" "...\v..." Vertical Tab
"" "" "...\\..." Litteral Baskslash \
"" "" "...\'..." Single quote
"" "" "...\nnn..." The eight-bit character whose value is the octal
value nnn (one to three digits)
"" "" "...\xHH..." The eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
"" "" "...\cx..." A <Control-x> character
PROMPT MANIPULATION
The shell PROMPT display can be modified by changing the value of the PS1 variable to
any desired text. The following special character combinations(\x) introduces the
corresponding entry into the PROMPT as well.
\a a bell character.
\d the date, in "Weekday Month Date" format (e.g., "Tue May 26").
\e an escape character.
\h the hostname, up to the first '.'
\H the hostname.
\n newline.
\s the name of the shell, the basename of $0 (the portion following the final slash).
\t the time, in 24-hour HH:MM:SS format.
\T the time, in 12-hour HH:MM:SS format.
\@ the time, in 12-hour am/pm format.
\v the version of Bash (e.g., 2.00)
\V the release of Bash, version + patchlevel (e.g., 2.00.0)
\w the current working directory.
\W the basename of $PWD.
\u your username.
\! the history number of this command.
\# the command number of this command.
\$ if the effective UID is 0, #, otherwise $.
\nnn the character corresponding to the octal number nnn.
\\ a backslash.
\[ begin a sequence of non-printing characters.
This could be used to embed a terminal control sequence into the prompt.
\] end a sequence of non-printing characters.
eg. PS1=\u@\h:\w > Could display a prompt as follows:
mario@topserver:/root >
68_Bash_Shell_Reference.sxw - 18
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
• - Start xterm
- in xterm start wterm in background (wterm &)
- Close xterm.....the wterm is also closed (owned by xterm)
• - Start xterm
- in xterm start wterm (wterm &)
- in xterm > jobs ....shows the background job
- in xterm > disown .... the last active job is disowned
- Close xterm.....the wterm is NOT closed.
Note: If in the original terminal mc is started, then some strange display of mc will
occur in the listening terminal unless the dimensions and fonts are the same as the
original terminal.
68_Bash_Shell_Reference.sxw - 19
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
Bash options:
Bash can be started with different options which alter the way bash works.
eg.1
set -o emacs Sets the emacs editing keys/commands:default
set -o vi Sets the vi editing keys/commands
eg.3 set -x
Sets bash in debugging mode. It will display the commands as they are
really executed by bash after bash has done its first scanning of the
command. This first scanning of the command is normally done to allow bash
to expand the file globing characters.
68_Bash_Shell_Reference.sxw - 20
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
68_Bash_Shell_Reference.sxw - 21
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
EXAMPLE COMMANDS
# Execute multiple commands on one line
pwd ; ls tmp ; echo "Hello world"
# Run the find command in the background
find . -name tmp.out -print &
# Connect the output of who to grep
who | grep fred
# Talk to fred if he is logged on
{ who | grep fred ; } && talk fred
# Send ls output to ls.out
ls > ls.out
# Append output of ls to ls.out
ls >> ls.out
# Send invite.txt to dick, jane, and spot
mail dick jane spot < invite.txt
# Send the standard error of xsend to stderr.out
xsend file 2>stderr.out
# List file names that begin with z
ls z*
# List two, three, and four character file names
ls ?? ??? ????
# List file names that begin with a, b, or c
ls [a-c]*
# List file names that do not end with .c
ls *[!.c]
# Set NU to the number of users that are logged on
NU=`who | wc -l` or NU=$(who | wc -l)
# Set TOTAL to the sum of 4 + 3
TOTAL=`expr 4 + 3` or TOTAL=$[4+3]
# Set and export the variable LBIN
LBIN=/usr/lbin; export LBIN
# Unset variable LBIN
unset LBIN
# Set SYS to the Operating System Name if not set, then display its value
echo ${SYS:=`uname -o`}
# Display an error message if XBIN is not set
: $X{BIN:?}
# Display $HOME set to /home/anatole
echo '$HOME set to' $HOME
# Display the value of $TERM
echo $TERM
# Bring background job 3 into the foreground
fg %3
# Stop the find job
stop %find
# Display the number of positional parameters
echo "There are $# positional parameters"
68_Bash_Shell_Reference.sxw - 22
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
68_Bash_Shell_Reference.sxw - 23
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
68_Bash_Shell_Reference.sxw - 24
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
Device Status
The following codes are used for reporting terminal/display settings, and vary depending
on the implementation:
Query Device Code echo -e \033[c
Terminal Setup
The h and l codes are used for setting terminal/display mode, and vary depending on the
implementation. Line Wrap is one of the few setup codes that tend to be used consistently:
Reset Device echo -e \033c
68_Bash_Shell_Reference.sxw - 25
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
• Text wraps to next line if longer than the length of the display area.
Disable Line Wrap echo -e \033[7l
Fonts
Some terminals support multiple fonts: normal/bold, swiss/italic, etc. There are a variety of
special codes for certain terminals; the following are fairly standard:
Font Set G0 echo -e \033(
Cursor Control
Cursor Home echo -e \033[{ROW};{COLUMN}H
• Sets the cursor position where subsequent text will begin. If no row/column
parameters are provided (ie. echo -e \033[H), the cursor will move to the home
position, at the upper left of the screen.
Cursor Up echo -e \033[{COUNT}A
68_Bash_Shell_Reference.sxw - 26
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
Scrolling
Scroll Screen echo -e \033[r
Tab Control
Set Tab echo -e \033
Erasing Text
Erase End of Line echo -e \033[K
• Erases from the current cursor position to the end of the current line.
Erase Start of Line echo -e \033[1K
• Erases from the current cursor position to the start of the current line.
Erase Line echo -e \033[2K
• Erases the screen from the current line down to the bottom of the screen.
68_Bash_Shell_Reference.sxw - 27
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
• Erases the screen from the current line up to the top of the screen.
Erase Screen echo -e \033[2J
• Erases the screen with the background color and moves the cursor to home.
Printing
Some terminals support local printing:
Print Screen echo -e \033[i
• Disable log.
Start Print Log echo -e \033[5i
Define Key
Set Key Definition echo -e \033[{key};"{string}"p
• Associates a string of text to a keyboard key. {key} indicates the key by its ASCII
value in decimal.
• Sets multiple display attribute settings. The following lists standard attributes:
0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden
Foreground Colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
68_Bash_Shell_Reference.sxw - 28
Linux-Kurs Themen – Bash Shell Reference- Feb 5, 2006 Michel Bisson
35 Magenta
36 Cyan
37 White
Background Colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
68_Bash_Shell_Reference.sxw - 29