Advanced Bash-Scripting Guide - Part 2. Basics
continued from Advanced Bash-Scripting Guide - Part 1. Introduction
Chapter 3. Special Characters
What makes a character special? If it has a meaning beyond its literal meaning, a meta-meaning, then we refer to it as a special character. Along with commands and keywords, special characters are building blocks of Bash scripts.
Special Characters Found In Scripts and Elsewhere
# Comments - Lines beginning with a #
(with the exception of #!
) are comments and will not be executed.
# This line is a comment.
Comments may also occur following the end of a command.
echo "A comment will follow." # Comment here. # ^ Note whitespace before #
Comments may also follow whitespace at the beginning of a line.
# A tab precedes this comment.
Comments may even be embedded within a pipe.
initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\ # Delete lines containing '#' comment character. sed -e 's/\./\. /g' -e 's/_/_ /g'` ) # Excerpted from life.sh script
A command may not follow a comment on the same line. There is no method of terminating the comment, in order for "live code" to begin on the same line. Use a new line for the next command.
Of course, a quoted or an escaped # in an echo statement does not begin a comment. Likewise, a # appears in certain parameter-substitution constructs and in numerical constant expressions.
echo "The # here does not begin a comment." echo 'The # here does not begin a comment.' echo The \# here does not begin a comment. echo The # here begins a comment. echo ${PATH#*:} # Parameter substitution, not a comment. echo $(( 2#101011 )) # Base conversion, not a comment. # Thanks, S.C.
The standard quoting and escape characters (" ' \) escape the #.
Certain pattern matching operations also use the #.
;
Command separator [semicolon]. Permits putting two or more commands on the same line.
echo hello; echo there if [ -x "$filename" ]; then # Note the space after the semicolon. #+ ^^ echo "File $filename exists."; cp $filename $filename.bak else # ^^ echo "File $filename not found."; touch $filename fi; echo "File test complete."
Note that the ";" sometimes needs to be escaped.
;;
Terminator in a case option [double semicolon].
case "$variable" in abc) echo "\$variable = abc" ;; xyz) echo "\$variable = xyz" ;; esac
;;&, ;&
Terminator in a case option [double semicolon].
case "$variable" in abc) echo "\$variable = abc" ;; xyz) echo "\$variable = xyz" ;; esac
;;&, ;&
Terminators in a case option (version 4+ of Bash).
.
"dot" command [period]. Equivalent to source (see Example 15-22). This is a bash builtin.
.
"dot", as a component of a filename. When working with filenames, a leading dot is the prefix of a "hidden" file, a file that an ls will not normally show.
bash$ touch .hidden-file bash$ ls -l total 10 -rw-r--r-- 1 bozo 4034 Jul 18 22:04 data1.addressbook -rw-r--r-- 1 bozo 4602 May 25 13:58 data1.addressbook.bak -rw-r--r-- 1 bozo 877 Dec 17 2000 employment.addressbook bash$ ls -al total 14 drwxrwxr-x 2 bozo bozo 1024 Aug 29 20:54 ./ drwx------ 52 bozo bozo 3072 Aug 29 20:51 ../ -rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.addressbook -rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.addressbook.bak -rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.addressbook -rw-rw-r-- 1 bozo bozo 0 Aug 29 20:54 .hidden-file
When considering directory names, a single dot represents the current working directory, and two dots denote the parent directory.
bash$ pwd /home/bozo/projects bash$ cd . bash$ pwd /home/bozo/projects bash$ cd .. bash$ pwd /home/bozo/
The dot often appears as the destination (directory) of a file movement command, in this context meaning current directory.
bash$ cp /home/bozo/current_work/junk/* .
Copy all the "junk" files to $PWD.
.
"dot" character match. When matching characters, as part of a regular expression, a "dot" matches a single character.
"
partial quoting [double quote]. "STRING" preserves (from interpretation) most of the special characters within STRING. See Chapter 5.
'
full quoting [single quote]. 'STRING' preserves all special characters within STRING. This is a stronger form of quoting than "STRING". See Chapter 5.
,
comma operator. The comma operator links together a series of arithmetic operations. All are evaluated, but only the last one is returned.
let "t2 = ((a = 9, 15 / 3))" # Set "a = 9" and "t2 = 15 / 3"
The comma operator can also concatenate strings.
for file in /{,usr/}bin/*calc # ^ Find all executable files ending in "calc" #+ in /bin and /usr/bin directories. do if [ -x "$file" ] then echo $file fi done # /bin/ipcalc # /usr/bin/kcalc # /usr/bin/oidcalc # /usr/bin/oocalc # Thank you, Rory Winston, for pointing this out.
,, ,
Lowercase conversion in parameter substitution (added in version 4 of Bash).
\/
escape [backslash]. A quoting mechanism for single characters.
\X escapes the character X. This has the effect of "quoting" X, equivalent to 'X'. The \ may be used to quote " and ', so they are expressed literally.
See Chapter 5 for an in-depth explanation of escaped characters.
/
Filename path separator [forward slash]. Separates the components of a filename (as in /home/bozo/projects/Makefile).
This is also the division arithmetic operator.
`
command substitution. The `command` construct makes available the output of command for assignment to a variable. This is also known as backquotes or backticks.
:
null command [colon]. This is the shell equivalent of a "NOP" (no op, a do-nothing operation). It may be considered a synonym for the shell builtin true. The ":" command is itself a Bash builtin, and its exit status is true (0).
: echo $? # 0
Endless loop:
while : do operation-1 operation-2 ... operation-n done # Same as: # while true # do # ... # done
Placeholder in if/then test:
if condition then : # Do nothing and branch ahead else # Or else ... take-some-action fi
Provide a placeholder where a binary operation is expected, see Example 8-2 and default parameters.
: ${username=`whoami`} # ${username=`whoami`} Gives an error without the leading : # unless "username" is a command or builtin... : ${1?"Usage: $0 ARGUMENT"} # From "usage-message.sh example script.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
We are in the process of converting a .html of the entire ABS guide to WIKI format so sections can be improved with ease. There is no more of the guide on this WIKI yet but there will be soon(-ish). The Full Guide is at https://linuxreviews.org/static/abs-guide.html - this is a 2.3 MB .html file. You can just click the link or wget for quick access when you need it.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!