A Tip A Day [:: ATAD ::]

a fortune, two cents a day

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
EOF                    # \hoo

cat <<EOF
EOF                    # \hoo

Source [ via Advanced Bash ]



Written by veed

August 13, 2008 at 8:50 pm

Posted in ATAD, linux, tech

Tagged with , ,

One Response

Subscribe to comments with RSS.

  1. ..] Thank you for reading this post. You can now Leave A Comment (0) or Leave A ..]


    September 6, 2008 at 1:06 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: