Buy this Shell Scripting Tutorial as a PDF for only $5
11. Variables - Part III
As we mentioned in Variables - Part I, curly brackets around a variable avoid confusion:
foo=sun echo $fooshine # $fooshine is undefined echo ${foo}shine # displays the word "sunshine"That's not all, though - these fancy brackets have a another, much more powerful use. We can deal with issues of variables being undefined or null (in the shell, there's not much difference between undefined and null).
Using Default Values
Consider the following code snippet which prompts the user for input, but accepts defaults:#!/bin/sh echo -en "What is your name [ `whoami` ] " read myname if [ -z "$myname" ]; then myname=`whoami` fi echo "Your name is : $myname"
Passing the "-en
" to echo tells it not to add a linebreak (for bash and csh). For Dash, Bourne and other compliant shells, you use a "\c
" at the end of the line, instead. Ksh understands both forms. (note: see /echo.html for a note on different implementations - particularly Dash/Bourne vs Bash)
This script runs like this if you accept the default by pressing "RETURN":
steve$ ./name.sh What is your name [ steve ] Your name is : steve
... or, with user input:
steve$ ./name.sh What is your name [ steve ] foo Your name is : fooThis could be done better using a shell variable feature. By using curly braces and the special ":-" usage, you can specify a default value to use if the variable is unset:
echo -en "What is your name [ `whoami` ] " read myname echo "Your name is : ${myname:-`whoami`}"This could be considered a special case - we're using the output of the whoami command, which prints your login name (UID). The more canonical example is to use fixed text, like this:
echo "Your name is : ${myname:-John Doe}"As with other use of the backticks,
`whoami`
runs in a subshell, so any cd
commands, or setting any other variables, within the backticks, will not affect the currently-running shell.
Using and Setting Default Values
There is another syntax, ":=", which sets the variable to the default if it is undefined:echo "Your name is : ${myname:=John Doe}"This technique means that any subsequent access to the
$myname
variable will always get a value, either entered by the user, or "John Doe" otherwise.


Back: Variables (Part 2) Next: External Programs
Books and eBooks
My Shell Scripting books, available in Paperback and eBook formats.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and to keep by your desk as an ever-present companion. | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half explains the features of the shell; the second half has real-world shell scripts, organised by topic, with detailed discussion of each script. |
Contact
You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!