You are currently viewing Shell Scripting – Special Characters
Shell Scripting – Special Characters

Shell Scripting – Special Characters

Shell Scripting – Special Characters

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed some basics of shell scripting and how to create and execute your 1st shell script.

https://cloudaffaire.com/shell-scripting-getting-started/

In this blog post, we will discuss special characters in the shell. Every character (apart from alphabets and numbers) has a unique meaning in the shell and is interpreted differently based on the format and its quite essential to know most of them to be an effective shell scripter.

Shell Scripting – Special Characters:

Hash (#):

The general use case of hash (#) is to define a comment in the shell but hash (#) can also be used for shebang, parameter substitution, and base conversion.

Semicolon (;):

The single semicolon (;) is used to execute multiple commands in a single line, the double semicolon (;;) is used as terminator in a case option.

Dot (.):

The single dot (.) represent different use case depending upon the usage in your script. It can be used to represent a hidden file, the current working directory, a single character replacement in regular expression. The double dot (..) is used to represent the parent directory of current working directory.

Quotes (“” and ”):

Double quotes (“”) represent string and preserves (from interpretation) most of the special characters within a string. The single quotes (”) represents string and preserves all special characters within a string. This is a stronger form of quoting than double-quotes.

Comma (,):

Comma (,) can be used to links together a series of arithmetic operations where all are evaluated, but only the last one is returned. The comma can also be used to concatenate strings.

Backslash (\):

Backslash (\) is used to escape other special characters in the shell.

Forward slash (/):

The forward-slash (/) is used as a filename path separator (/some/path). It also represents the root directory. In arithmetic operation forward slash means division.

Colon (:):

In shell, a colon (:) can be used as a null command, endless loop, a place holder, for string evaluation, for field separator or to empty a file.

Exclamation (!):

Bang (!) can be used to check the reverse of equality (not equal). Bang (!) can also be used to invert the exit status.

Asterisk (*):

Asterisk (*) can be used as a “wild card” for filename expansion in globbing. By itself, it matches every filename in a given directory. Asterisk (*) also represents any number (or zero) characters in a regular expression. In arithmetic operation single asterisk (*) represents multiplication and double asterisk (**) represents exponentiation.

Question mark (?):

Question mark (?) or test operator is used within certain expressions to indicates a test for a condition. Question mark (?) or test operator is also used to test if a variable value has been set. Question mark (?) in the regular expression is used as a single-character wild card.

Dollar ($):

A single dollar ($) prefixing a variable name indicates the value the variable holds. A single dollar ($) in a regular expression represents a match from the end of the line. “XXX$” matches XXX at the end of a line. A single dollar followed by a curly brace (${}) represents parameter substitution. A single dollar followed by a single quote ($”) can be used as a quoted-string expansion construct. A single dollar ($) followed by different character represents positional parameters and special meanings.

Round brackets (()):

Round brackets (()) is used for grouping of multiple commands. Round brackets prefix by dollar ($()) represents parameter substitution. Round brackets (()) is also used to initialize an array. Round brackets (()) can also be used to initiate sub process.

Curly brackets ({}):

Curly brackets ({}) can be used to build a sequence using extended brace expansion. Curly brackets ({}) can also be used for parameter substitution. Curly brackets ({}) can also be used to create a function without any name.

Square brackets ([]):

Square brackets ([]) is used for globbing in case of regular expressions. Square brackets ([]) is also used to evaluate a test condition. Square brackets ([]) is also used to reference array element. Dollar followed by square brackets ($[]) can be used for integer expansion. Double Square brackets ([[]]) is used to evaluate a test condition and is more flexible than single square brackets ([]).

Greater than (>):

Greater than (>) is used for redirection to file. Double Greater than (>>) can be used for append to file. Greater than (>) can also be used file truncation. Greater than (>) can also be used as a comparison operator in certain cases.

Less than (<):

Double less than (<<) is used as heredoc. Less than (<) can be used as a comparison operator in certain cases. Greater than and less than with prefix forward-slash (\<\>) is used as word boundary in the regular expression.

Pipe (|):

Pipe (|) is a method to send the output of one command as an input to another command. Pipe (|) can also be used to force redirection. Double pipe (||) is used as a logical OR operator in test construct.

Ampersand (&):

A single ampersand (&) is used to run a job in the background. A double ampersand (&&) represents the logical AND operator.

Plus (+):

Plus (+) represent an addition in arithmetic operations. Plus (+) can also be used to append string.

Minus (-):

Minus (-) represent subtraction in arithmetic operations. Minus or hyphen (-) is also used to go back to the previous working directory. The single or double hyphen (- or –) is also used to pass options to the command.

Percent (%):

Percent (%) is used as a modulo operator in arithmetic operations.

Tilde (~):

Tilde (~) represents your home directory. Tilde with plus suffix (~+) represents the current working directory and tilde with minus suffix (~-) represent the previous working directory.

Equals to (=):

Equals to (=) is used for variable assignment. Equals to (=) is also used for comparison.

Caret (^):

Caret (^) represents the beginning of a line in the regular expression. Caret (^) can also be used for uppercase conversion.

Back quote ():

The backquote () is used for command substitution.

Hope you have enjoyed this blog post. This blog post is inspired by the Advanced Bash-Scripting Guide written by Mendel Cooper. You can refer to his amazing series using the below link.

http://www.tldp.org/LDP/abs/html/

 

Leave a Reply