| 1 | #!/bin/bash |
| 2 | # escaped.sh: escaped characters |
| 3 | |
| 4 | ############################################################# |
| 5 | ### First, let's show some basic escaped-character usage. ### |
| 6 | ############################################################# |
| 7 | |
| 8 | # Escaping a newline. |
| 9 | # ------------------ |
| 10 | |
| 11 | echo "" |
| 12 | |
| 13 | echo "This will print |
| 14 | as two lines." |
| 15 | # This will print |
| 16 | # as two lines. |
| 17 | |
| 18 | echo "This will print \ |
| 19 | as one line." |
| 20 | # This will print as one line. |
| 21 | |
| 22 | echo; echo |
| 23 | |
| 24 | echo "=============" |
| 25 | |
| 26 | |
| 27 | echo "\v\v\v\v" # Prints \v\v\v\v literally. |
| 28 | # Use the -e option with 'echo' to print escaped characters. |
| 29 | echo "=============" |
| 30 | echo "VERTICAL TABS" |
| 31 | echo -e "\v\v\v\v" # Prints 4 vertical tabs. |
| 32 | echo "==============" |
| 33 | |
| 34 | echo "QUOTATION MARK" |
| 35 | echo -e "\042" # Prints " (quote, octal ASCII character 42). |
| 36 | echo "==============" |
| 37 | |
| 38 | |
| 39 | |
| 40 | # The $'\X' construct makes the -e option unnecessary. |
| 41 | |
| 42 | echo; echo "NEWLINE and (maybe) BEEP" |
| 43 | echo $'\n' # Newline. |
| 44 | echo $'\a' # Alert (beep). |
| 45 | # May only flash, not beep, depending on terminal. |
| 46 | |
| 47 | # We have seen $'\nnn" string expansion, and now . . . |
| 48 | |
| 49 | # =================================================================== # |
| 50 | # Version 2 of Bash introduced the $'\nnn' string expansion construct. |
| 51 | # =================================================================== # |
| 52 | |
| 53 | echo "Introducing the \$\' ... \' string-expansion construct . . . " |
| 54 | echo ". . . featuring more quotation marks." |
| 55 | |
| 56 | echo $'\t \042 \t' # Quote (") framed by tabs. |
| 57 | # Note that '\nnn' is an octal value. |
| 58 | |
| 59 | # It also works with hexadecimal values, in an $'\xhhh' construct. |
| 60 | echo $'\t \x22 \t' # Quote (") framed by tabs. |
| 61 | # Thank you, Greg Keraunen, for pointing this out. |
| 62 | # Earlier Bash versions allowed '\x022'. |
| 63 | |
| 64 | echo |
| 65 | |
| 66 | |
| 67 | # Assigning ASCII characters to a variable. |
| 68 | # ---------------------------------------- |
| 69 | quote=$'\042' # " assigned to a variable. |
| 70 | echo "$quote Quoted string $quote and this lies outside the quotes." |
| 71 | |
| 72 | echo |
| 73 | |
| 74 | # Concatenating ASCII chars in a variable. |
| 75 | triple_underline=$'\137\137\137' # 137 is octal ASCII code for '_'. |
| 76 | echo "$triple_underline UNDERLINE $triple_underline" |
| 77 | |
| 78 | echo |
| 79 | |
| 80 | ABC=$'\101\102\103\010' # 101, 102, 103 are octal A, B, C. |
| 81 | echo $ABC |
| 82 | |
| 83 | echo |
| 84 | |
| 85 | escape=$'\033' # 033 is octal for escape. |
| 86 | echo "\"escape\" echoes as $escape" |
| 87 | # no visible output. |
| 88 | |
| 89 | echo |
| 90 | |
| 91 | exit 0 |