Special characters in BASH scripting

Before jumping into BASH scripting, it is very important to know special characters in BASH.

Here is the list of special characters in BASH.

  1. # -> This character (hash) is used for marking the comments in shell script
  2. ; -> This character (semi-colon) is used to separate 2 BASH commands.
  3. ;; -> This is used in select case construct to mark the end of case block.
  4. . -> This character (dot) is used to indicate current directory. If used in regular expressions, it matches exactly one character. The files with names starting with “.” are treated as hidden files.
  5. ” ” and ‘ ‘ -> Double quotes are used to preserve the spaces and identify special characters. Single quotes are used to preserve everything as it is. Variable value substitution does not work
  6. / -> This character (forward slash) is used as path separator. For example – /etc/passwd. This character is also used as division operator in arithmetic operations.
  7. \ -> This character (backward slash) is used to escape following character
  8. ` -> backquote is used to execute the command and return the result
  9. : -> This character(colon) is used as field separator in /etc/passwd file and in PATH variable
  10. ! -> This character (exclamation mark) is used to invoke commands from bash history. This is also used as not equal to operator (!=)
  11. * -> This character (star or asterisk) is used to match anything during filename expansion. This is also used in regular expressions to match zero or more occurrences of preceding character. Additionally this character is also used as multiplication operator.
  12. ** -> This set of characters are used as exponentiation operator.
  13. ? -> This character is used as condition operator. In regular expressions, it matches o or 1 character.
  14. $ -> This character is used as variable substitution. In regular expressions, it matches end of line.
  15. $*, [email protected] -> These set of characters are used to access all positional parameters in one go. We can access each argument separately like $0 – script name, $1 – first parameter …and so on.
  16. $? -> This is used to access the exit status of last BASH command.
  17. $$ -> This is used to get the current process id.
  18. = -> This is used during assignment of value to the variable. This is also used to compare 2 strings or integers.
  19. < -> This symbol is used as Input redirection. It is also used as “less than” comparison operator.
  20. > -> This symbol is used as output redirection. It is also used as “greater than” comparison operator.
  21. >> -> This symbol is used as output redirection. Data is appended to the file.
  22. & -> This character is used to run job in the background. For example – “ls &”
  23. – -> This character is used as minus sign in arithmetic operations. It is also used in cd command like “cd -” which takes you to previous working directory.
  24. + -> This is used as addition operator in arithmetic operations.

Now let use look at some more complex special characters.

, -> This character is used for concatenation of strings.

$() -> Used for command substitution. Command inside parenthesis is executed and assigned to variable on left side. for example data=$(ls). This is also used for defining functions. This is also used for declaring array.

{} -> They are used for parameter expansions. They are also used in xargs and find command as well. For example – echo {a..e}  This command will print values a, b,c,d,e

[] -> This is used to access array element. They are also used to define the character classes. They are also used in condition statements. Some of the operators are -eq, -lt, -gt, -a (logical and operator), -o (logical or operator).

((   )) -> Used to do arithmetic operations. Note that you do not need to use $ sign to access the variable values inside parenthesis.

[[ ]] -> This is used in conditional statements. You can use logical and operator (&&), logical or operator (||) and  =~ (regular expression operator).

You may also like...