LDP/LDP/guide/docbook/abs-guide/escaped.sh

92 lines
2.3 KiB
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2001-09-04 13:27:31 +00:00
# escaped.sh: escaped characters
2011-05-03 15:38:48 +00:00
#############################################################
### First, let's show some basic escaped-character usage. ###
#############################################################
2006-12-20 21:11:55 +00:00
# Escaping a newline.
# ------------------
echo ""
echo "This will print
as two lines."
# This will print
# as two lines.
echo "This will print \
as one line."
# This will print as one line.
echo; echo
echo "============="
2002-09-30 15:56:52 +00:00
echo "\v\v\v\v" # Prints \v\v\v\v literally.
2002-04-01 16:04:17 +00:00
# Use the -e option with 'echo' to print escaped characters.
2002-09-30 15:56:52 +00:00
echo "============="
echo "VERTICAL TABS"
2001-07-10 14:25:50 +00:00
echo -e "\v\v\v\v" # Prints 4 vertical tabs.
2002-09-30 15:56:52 +00:00
echo "=============="
echo "QUOTATION MARK"
2001-09-04 13:27:31 +00:00
echo -e "\042" # Prints " (quote, octal ASCII character 42).
2002-09-30 15:56:52 +00:00
echo "=============="
2001-07-10 14:25:50 +00:00
2011-05-03 15:38:48 +00:00
2002-04-01 16:04:17 +00:00
# The $'\X' construct makes the -e option unnecessary.
2011-05-03 15:38:48 +00:00
2011-08-29 23:59:19 +00:00
echo; echo "NEWLINE and (maybe) BEEP"
2002-04-01 16:04:17 +00:00
echo $'\n' # Newline.
echo $'\a' # Alert (beep).
2011-08-29 23:59:19 +00:00
# May only flash, not beep, depending on terminal.
2001-07-10 14:25:50 +00:00
2011-08-29 23:59:19 +00:00
# We have seen $'\nnn" string expansion, and now . . .
2011-05-03 15:38:48 +00:00
# =================================================================== #
# Version 2 of Bash introduced the $'\nnn' string expansion construct.
# =================================================================== #
2011-08-29 23:59:19 +00:00
echo "Introducing the \$\' ... \' string-expansion construct . . . "
echo ". . . featuring more quotation marks."
2011-05-03 15:38:48 +00:00
2001-09-04 13:27:31 +00:00
echo $'\t \042 \t' # Quote (") framed by tabs.
2011-05-03 15:38:48 +00:00
# Note that '\nnn' is an octal value.
2001-07-10 14:25:50 +00:00
2002-09-30 15:56:52 +00:00
# It also works with hexadecimal values, in an $'\xhhh' construct.
2003-01-06 21:25:36 +00:00
echo $'\t \x22 \t' # Quote (") framed by tabs.
2002-09-30 15:56:52 +00:00
# Thank you, Greg Keraunen, for pointing this out.
2003-01-06 21:25:36 +00:00
# Earlier Bash versions allowed '\x022'.
2011-05-03 15:38:48 +00:00
2002-09-30 15:56:52 +00:00
echo
2002-04-01 16:04:17 +00:00
2001-07-10 14:25:50 +00:00
# Assigning ASCII characters to a variable.
# ----------------------------------------
2001-09-04 13:27:31 +00:00
quote=$'\042' # " assigned to a variable.
2011-08-29 23:59:19 +00:00
echo "$quote Quoted string $quote and this lies outside the quotes."
2001-09-04 13:27:31 +00:00
echo
2001-07-10 14:25:50 +00:00
# Concatenating ASCII chars in a variable.
2002-04-01 16:04:17 +00:00
triple_underline=$'\137\137\137' # 137 is octal ASCII code for '_'.
2001-07-10 14:25:50 +00:00
echo "$triple_underline UNDERLINE $triple_underline"
2002-09-30 15:56:52 +00:00
echo
2001-09-04 13:27:31 +00:00
ABC=$'\101\102\103\010' # 101, 102, 103 are octal A, B, C.
echo $ABC
2011-08-29 23:59:19 +00:00
echo
2001-09-04 13:27:31 +00:00
escape=$'\033' # 033 is octal for escape.
echo "\"escape\" echoes as $escape"
2002-04-01 16:04:17 +00:00
# no visible output.
2001-09-04 13:27:31 +00:00
2011-08-29 23:59:19 +00:00
echo
2001-09-04 13:27:31 +00:00
2001-07-10 14:25:50 +00:00
exit 0