27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 5−1. Echoing Weird Variables<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

#!/bin/bash<br />

# weirdvars.sh: Echoing weird variables.<br />

var="'(]\\{}\$\""<br />

echo $var # '(]\{}$"<br />

echo "$var" # '(]\{}$" Doesn't make a difference.<br />

echo<br />

IFS='\'<br />

echo $var # '(] {}$" \ converted to space.<br />

echo "$var" # '(]\{}$"<br />

# Examples above supplied by S.C.<br />

exit 0<br />

Single quotes (' ') operate similarly to double quotes, but do not permit referencing variables, since the special<br />

meaning of $ is turned off. Within single quotes, every special character except ' gets interpreted literally.<br />

Consider single quotes ("full quoting") to be a stricter method of quoting than double quotes ("partial<br />

quoting").<br />

Since even the escape character (\) gets a literal interpretation within single quotes, trying to enclose<br />

a single quote within single quotes will not yield the expected result.<br />

echo "Why can't I write 's between single quotes"<br />

echo<br />

# The roundabout method.<br />

echo 'Why can'\''t I write '"'"'s between single quotes'<br />

# |−−−−−−−| |−−−−−−−−−−| |−−−−−−−−−−−−−−−−−−−−−−−|<br />

# Three single−quoted strings, with escaped and quoted single quotes between.<br />

# This example courtesy of Stephane Chazelas.<br />

Escaping is a method of quoting single characters. The escape (\) preceding a character tells the shell to<br />

interpret that character literally.<br />

With certain commands and utilities, such as echo and sed, escaping a character may have the opposite<br />

effect − it can toggle on a special meaning for that character.<br />

Special meanings of certain escaped characters<br />

used with echo and sed<br />

\n<br />

means newline<br />

\r<br />

means return<br />

\t<br />

means tab<br />

\v<br />

means vertical tab<br />

Chapter 5. Quoting 33

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!