A Tip A Day [:: ATAD ::]

a fortune, two cents a day

Posts Tagged ‘bash

ATAD #16 – escape sequence in bash

with one comment

Certain characters are processed by the shell, and can be escaped (\) to tell the shell to interpret that literally. Of note are the characters '$', '`', '\', and '"'

echo "Hello Word"          #output: Hello Word
echo "\"Hello World\""     #output: "Hello World"

With certain utilities like echo and sed, escaping a character may have the opposite effect – it can toggle on a special meaning for that character.

\n means newline means return
\t means tab
\v means vertical tab
\b means backspace
\a means “alert” (beep or flash)
\d the date in “Weekday Month Date” format
\e an ASCII escape character (033)
\H the hostname
\j the number of jobs currently managed by the shell
\s the name of the shell, the basename of $0
\t the current time in 24-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format

echo -e "\v\v\v\v"     # Prints 4 vertical tabs.
# The $'\X' construct makes the -e option unnecessary.
echo $'\n'             # Newline.

The behavior of \ depends on whether it is itself escaped, quoted, or appearing within command substitution

# escaping and quoting
echo \hoo              # hoo
echo \\hoo             # \hoo
echo '\hoo'            # \hoo
echo '\\hoo'           # \\hoo
echo "\hoo"            # \hoo
echo "\\hoo"           # \hoo

# Command substitution
echo `echo \hoo`       # hoo
echo `echo \\hoo`      # hoo
echo `echo \\\hoo`     # \hoo
echo `echo \\\\hoo`    # \hoo
echo `echo \\\\\\\hoo` # \\hoo
echo `echo "\hoo"`     # \hoo
echo `echo "\\hoo"`    # \hoo


# Here document
cat <<EOF
\hoo
EOF                    # \hoo

cat <<EOF
\\hoo
EOF                    # \hoo

Source [ via Advanced Bash ]

__tipped__

Written by vinaydeep

August 13, 2008 at 8:50 pm

Posted in ATAD, linux, tech

Tagged with , ,