Home
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.
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.
Comments or Hash (#)
This Special character (#) is used for
adding lines to the script which will not be executed, a recent example can be
Sha-Bang
Below are various scenarios of using
Comment with different meaning for each one,
# This line is a comment.
Comment at start of line, making whole
line not executable.
echo "A comment will follow" # Comment here.
Comment at end part of line, making
part of line not executable.
echo "The # here does not begin a comment."
Comment used as Text for printing in
output, within double quotes
echo The \# here does not begin a comment.
Comment used as Text for printing in
output, by escaping it
echo The # here begins a comment.
Comment used without double quotes
echo ${PATH#*:}
Comment used as parameter substitution
echo $(( 2#101011 )); echo $(( 6#101 ))
Comment used for conversion
Command separator or Semicolon (;)
Semicolon permits two commands on same
line, it’s a command separator to be used in command line as well as inside a
script.
echo hello; echo there
Terminator or double Semicolon (;;)
This is mainly used in case statement
to show that commands to be executed as a part of a case criteria are finished.
i.e. end of logic to be executed when a case is matched as script execution
goes in sequence line by line.
case "$variable" in
abc) echo "\$variable =
abc" ;;
xyz) echo "\$variable =
xyz" ;;
esac
Dot (.)
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 sam sam 4034 Jul 18 22:04 file1
-rw-r--r--
1 sam sam 4602 May 25 13:58 file2
-rw-r--r--
1 sam sam 877 Dec 17 2000 file3
bash$ ls -al
total 14
drwxrwxr-x
2 sam sam 1024 Aug 29 20:54 ./
drwx------
52 sam sam 3072 Aug 29 20:51 ../
-rw-r--r--
1 sam sam 4034 Jul 18 22:04 file1
-rw-r--r--
1 sam sam 4602 May 25 13:58 file2
-rw-r--r--
1 sam sam 877 Dec 17 2000 file3
-rw-rw-r--
1 sam sam 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/sam/projects
bash$ cd .
bash$ pwd
/home/sam/projects
bash$ cd ..
bash$ pwd
/home/sam/
The dot often appears as the
destination (directory) of a file movement command, in this context meaning
current directory. Copy all the "junk" files to $PWD.
$ cp
/home/sam/current_work/junk/* .
File name path separator or forward
slash (/)
Separates the components of a filename
(as in /home/sam/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.
$ echo $value # NULL
$ pwd
$
value=`pwd`
$ echo $value # Path obtained in pwd command output
Null command or colon (:)
This is the shell
equivalent of a "NOP" (no op, a do-nothing operation).
The ":" command
is itself a Bash built-in and its exit status is true (0).
$ :
$ echo $?
# 0
Endless loop using “:” can
be constructed as below,
while : ; do
echo `date`; sleep 20; done;
Same as that of
traditional approach of writing loop as below,
while true ;
do echo `date`; sleep 20; done;
Placeholder in if/then
test:
if condition
then : # Do nothing and branch ahead
else # Or else ...
some-action
fi
The ":" serves as a field
separator in some OS level config files, an example can be /etc/passwd, and in
the $PATH from environment variables.
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/usr/games
Reverse (or negate) the sense of a
test or exit status or exclamation (!)
The (!) operator inverts
the exit status of the command to which it is applied.
It also inverts the
meaning of a test operator.
For example, change the
sense of equal ( = ) to not-equal ( != ).
Wild card or asterisk (*)
The * character serves as
a "wild card" for filename expansion. By itself, it matches every
filename in a given directory.
$ echo * # gives name of files and folders in a
particular directory
$ ls /* # gives list of all folders and files
within / along with listing of content of subdirectories to one level of /
In the context of arithmetic
operations, the * denotes multiplication.
Dollar ($)
Character ($) is used for
variable substitution or showing contents of a variable
var1=5
var2=23skidoo
echo
$var1 # 5
A $ prefixing a variable
name indicates the value the variable holds.
echo
$var2 # 23skidoo
$ also indicates
end-of-line. In a regular expression, a "$" addresses the end of a
line of text.
find /
-ls |awk '{print $3, $NF}' |grep Z$
# files engins with letter Z
${} can be used for
parameter substitution.
$' ... ' i.e. Quoted
string expansion. This construct expands single or multiple escaped octal or
hex values
into ASCII [3] or Unicode
characters.
$*, $@ are used as
positional parameters.
$? provides exit status
variable. The $? Variable holds the exit status of a command, a function, or of
the script itself.
$ :
$ echo $?
# 0
$$ is a process ID
variable. The $$ variable holds the process ID of the script in which it
appears. below steps can be tried to understand its usage inside a script.
Create a file test.sh with
below content.
#!/bin.bash
while :
do
echo $$ >> test.out.log
sleep 10
done
Execute file in background and observe
the PID obtained when you get it ran in background
# sh test.sh
>> test.out.log 2>&1
Once executed in background check
output file test.out.log, it will keep on capturing a PID which should match
with PID that you observed while getting job added to background for execution.
# cat
test.out.log
Redirection (> &> >& >>
< <>)
# command
> out.log
Redirects the output of command
to file out.log. Overwrite filename if it already exists. As error file not
specified it will be shown on console.
# command
> out.log 2> error.log
Redirects the output of command
to file out.log and error of command to file error.log. Overwrite filename if
it already exists.
command
&> out.log
Redirects both the stdout
and the stderr of command to file out.log.
command >
out.log 2>&1
Another way to redirect
both the stdout and the stderr of command to file out.log.
command
>&2
Redirects stdout of
command to stderr. As error file not specified it will be shown on console.
command 2>
error.log >&2
Redirects stdout of
command to stderr. As error file not specified it will be shown on console.
command
>> out.log
Appends the output of scriptname
to file filename. If filename does not already exist, it is created.
[i]<>in.file
Opens file in.file for
reading and writing, and assigns file descriptor i to it. If filename does not
exist, it is created.
Pipe (|)
This is a very frequently
special character. Pipe passes the output (stdout) of a previous command to the
input (stdin) of the next one, or to the shell.
echo ls -l |
sh
Passes the output of "echo ls
-l" to the shell, with the same result as a simple "ls -l".
cat *.lst |
sort | uniq
Merges and sorts all ".lst"
files, then deletes duplicate lines.
OR (||)
OR logical operator. In a test
construct, the || operator causes a return of 0 (success) if either of the
linked test conditions is true.
Single and (&)
Run job in background. A command
followed by an & will run in the background.
$ sleep 10
&
[1] 850
[1]+ Done
sleep 10
Double and (&&)
AND logical operator. In a
test construct, the && operator causes a return of 0 (success) only if
both the linked test conditions are true.
Option, prefix (-)
Option flag for a command
or filter. Prefix for an operator. Prefix for a default parameter in parameter
substitution.
COMMAND
-[Option1][Option2][...]
ls -al
sort -dfu
$filename
Home directory or tilde (~)
This corresponds to the $HOME internal
variable. ~sam is sam's home directory, and ls ~sam lists the contents of it.
~/ is the current user's home directory, and ls ~/ lists the contents of it.
$ echo ~bozo
/home/bozo
$ echo ~
/home/bozo
$ echo ~/
/home/bozo/
$ echo ~:
/home/sam:
Escape or backslash (\)
One of the most important
special character. In order to use other special characters well in either on
command line or within script combined use of Escape and other special
character is required. This concept it termed as escaping your special
character.
Basic example can be below
to escape # and print it in output.
No comments:
Post a Comment