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.

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

a=`echo "HELLO" | tr A−Z a−z`<br />

See also Example 12−15, Example 12−2, Example 12−32, and Example 12−33.<br />

Be aware that echo `command` deletes any linefeeds that the output of command generates.<br />

The $IFS (internal field separator) variable normally contains \n (linefeed) as one of its set of<br />

whitespace characters. Bash therefore splits the output of command at linefeeds into arguments to<br />

echo. Then echo outputs these arguments, separated by spaces.<br />

bash$ ls −l /usr/share/apps/kjezz/sounds<br />

−rw−r−−r−− 1 root root 1407 Nov 7 2000 reflect.au<br />

−rw−r−−r−− 1 root root 362 Nov 7 2000 seconds.au<br />

bash$ echo `ls −l /usr/share/apps/kjezz/sounds`<br />

total 40 −rw−r−−r−− 1 root root 716 Nov 7 2000 reflect.au −rw−r−−r−− 1 root root 362 Nov 7<br />

This command is a shell builtin, and not the same as /bin/echo, although its<br />

behavior is similar.<br />

bash$ type −a echo<br />

echo is a shell builtin<br />

echo is /bin/echo<br />

printf<br />

The printf, formatted print, command is an enhanced echo. It is a limited variant of the C language<br />

printf() library function, and its syntax is somewhat different.<br />

printf format−string... parameter...<br />

This is the Bash builtin version of the /bin/printf or /usr/bin/printf command. See the<br />

printf manpage (of the system command) for in−depth coverage.<br />

Older versions of Bash may not support printf.<br />

Example 11−1. printf in action<br />

#!/bin/bash<br />

# printf demo<br />

PI=3.14159265358979<br />

DecimalConstant=31373<br />

Message1="Greetings,"<br />

Message2="Earthling."<br />

echo<br />

printf "Pi to 2 decimal places = %1.2f" $PI<br />

echo<br />

Chapter 11. Internal Commands and Builtins 125

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

Saved successfully!

Ooh no, something went wrong!