Special characters in bash evaluated with special meaning in interpretion of a command. These characters has special instructions, on using this characters has different meanings on different contexts
Let’s expore supported special characters in bash scripting
Blankspace(" “):
These also called whitespaces, contains tab,spaces,return, new line. These tells bash interepreter to separate the commands and content. It is an delimeter to separate a commands as well as strings.
echo "Hello World"
The above example, echo is an command followed by a space, and strign contains spaces for words
Expansion($)
Dollar sign symbol used for different types of expansions
parameter expansion, ($variable, ${variable}
)
Substitution ($(expression)
)
arthematic expressions ($((expression))
)
Ambersand (&
)
Adding &
to an end of a command allows you to execute the command in the background.
command $
For example, To run redis server in the background use, below command
redis-server &
Pipe (|
)
It is used to pass the output of one command to an input to another command from left to right. It allows to fomr a chain of commands
Syntax is command1 | command2
Example : echo "hello" | wc
returns the character count.
Semicolon(;)
It is used to separate a multiple commands using ;
in a single line.;
is an command separator to define multiple commands in a single line
Syntax: command1; command2;command3
Example: cd /app/;ls;
Single quotes
Single quotes ('
) are used to define a string without a special meaning.
It means all the variables and expansion are not interepreted and print the same literal string
name="Eric"
echo "Hi, $name" # Hi, Eric
echo 'Hello, $name' # Hi $name
Example, First echo, name variable is expanded and interpreted as string and printed. Second echo, uses single quotes, and name variable is not expanded and printed as literal string.
If single quote contains an nested single quoe, You need to escape it using ```.
echo 'test string with nested single quote: '\''Hello'\'''
Examle contains ``` is an single quote character inside a single quote
Double quotes
Double quotes ('
) are used to define a string literals with a special meaning.
if string contains a variables and expansion syntax, These are intereprested and expanded, with a evaluated value at runtime.
if string don’t want to expand their variables, then you can escape \
before $
dollar symbol
name="Eric"
echo "Hi, $name" # Hi, Eric
echo "Hi, \$name" # Hi, $name
Example, First echo, name variable is expanded and interpreted as string and printed. Second echo, $ prefixed escape character \
, printed as literal string.
Backslash Character(\
)
Backslash character used to escape the characters in strings. it is used in double quoted strings.
For example
echo escape $$ example # escape 3225 example
echo escape \$$ example # escape $$ example
In the first example, echo contains $$
, which displays process id.
In the second example, echo contains \$$
, which displays $$
as literal string. escape character prefixed.
Comment ( #
)
Comments symbole used to comment out a line of code. Line of comment always starts with #
.
It is ignored by bash interpreter.
# Line comment
echo "comment example" # Inline comment
Question Mark(?
)
question mark has a different meanings in context.
- In regular expression context
- In
check the exit status of a last command execution.