Skip to content

Special characters in bash

#

  • Comments
# This line is a comment.

echo "A comment will follow." # Comment here.
#                            ^ Note whitespace before #

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.

;

  • Command separator
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."

;;

  • Terminator in a case option
case "$variable" in
  abc)  echo "\$variable = abc" ;;
  xyz)  echo "\$variable = xyz" ;;
esac

;;&, ;&: Terminators in a case option (version 4+ of Bash).

.

  • Equivalent to source (a bash builtin)
  • as a component of a filename (“hidden” file)
  • character match (matche a single character)

,

  • comma operator (link together a series of arithmetic operations)
let "t2 = ((a = 9, 15 / 3))"
# Set "a = 9" and "t2 = 15 / 3"
  • 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

\

  • escape (backslash). A quoting mechanism for single characters.

/

  • Filename path separator (forward slash).

:

  • null command, a Bash builtin and its exit status is true (0)
  • Endlees 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
: ${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.
  • Evaluate string of variables using parameter substitution
: ${HOSTNAME?} ${USER?} ${MAIL?}
#  Prints error message
#+ if one or more of essential environmental variables not set.
  • Variable expansion / substring replacement
: > data.xxx   # File "data.xxx" now empty.
# Same effect as   cat /dev/null >data.xxx
# However, this does not fork a new process, since ":" is a builtin.
  • as a function name
:() {
  echo "The name of this function is "$FUNCNAME" "
  # Why use a colon as a function name?
  # It's a way of obfuscating your code.
}
:
# The name of this function is :
  • serve as a placeholder in an otherwise empty function
not_empty ()
{
  :
} # Contains a : (null command), and so is not empty.

Reference

  • Advanced Bash-Scripting Guide - An in-depth exploration of the art of shell scripting (Mendel Cooper)

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback: dig +short txt issue.imzye.com
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Buy Me a Coffee