12.07.2015 Views

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

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.

<strong>Advanced</strong> <strong>Bash</strong>-<strong>Scripting</strong> <strong>Guide</strong>COMMAND "This is $variable1" # Executes COMMAND with 1 argument:# "This is a variable containing five words"variable2=""# Empty.COMMAND $variable2 $variable2 $variable2# Executes COMMAND with no arguments.COMMAND "$variable2" "$variable2" "$variable2"# Executes COMMAND with 3 empty arguments.COMMAND "$variable2 $variable2 $variable2"# Executes COMMAND with 1 argument (2 spaces).# Thanks, Stéphane Chazelas.Enclosing the arguments to an echo statement in double quotes is necessary only when word splitting orpreservation of whitespace is an issue.Example 5-1. Echoing Weird Variables#!/bin/bash# weirdvars.sh: Echoing weird variables.var="'(]\\{}\$\""echo $var # '(]\{}$"echo "$var" # '(]\{}$" Doesn't make a difference.echoIFS='\'echo $var # '(] {}$" \ converted to space. Why?echo "$var" # '(]\{}$"# Examples above supplied by Stephane Chazelas.exit 0Single quotes (' ') operate similarly to double quotes, but do not permit referencing variables, since the specialmeaning of $ is turned off. Within single quotes, every special character except ' gets interpreted literally.Consider single quotes ("full quoting") to be a stricter method of quoting than double quotes ("partialquoting").Since even the escape character (\) gets a literal interpretation within single quotes, trying to enclose asingle quote within single quotes will not yield the expected result.echo "Why can't I write 's between single quotes"echo# The roundabout method.echo 'Why can'\''t I write '"'"'s between single quotes'# |-------| |----------| |-----------------------|# Three single-quoted strings, with escaped and quoted single quotes between.# This example courtesy of Stéphane Chazelas.Chapter 5. Quoting 38

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

Saved successfully!

Ooh no, something went wrong!