<HTML ><HEAD ><TITLE >Variables</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Bash Prompt HOWTO" HREF="index.html"><LINK REL="UP" TITLE="Bash Programming and Shell Scripts" HREF="c189.html"><LINK REL="PREVIOUS" TITLE="Bash Programming and Shell Scripts" HREF="c189.html"><LINK REL="NEXT" TITLE="Quotes and Special Characters" HREF="x206.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Bash Prompt HOWTO: </TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="c189.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 3. Bash Programming and Shell Scripts</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x206.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN191" ></A >3.1. Variables</H1 ><P >I'm not going to try to explain all the details of Bash scripting in a section of this HOWTO, just the details pertaining to prompts. If you want to know more about shell programming and Bash in general, I highly recommend <EM >Learning the Bash Shell</EM > by Cameron Newham and Bill Rosenblatt (O'Reilly, 1998). Oddly, my copy of this book is quite frayed. Again, I'm going to assume that you know a fair bit about Bash already. You can skip this section if you're only looking for the basics, but remember it and refer back if you proceed much farther. </P ><P >Variables in Bash are assigned much as they are in any programming language:</P ><TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" >testvar=5 foo=zen bar="bash prompt"</PRE ></FONT ></TD ></TR ></TABLE ><P >Quotes are only needed in an assignment if a space (or special character, discussed shortly) is a part of the variable.</P ><P >Variables are referenced slightly differently than they are assigned:</P ><TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" >> echo $testvar 5 > echo $foo zen > echo ${bar} bash prompt > echo $NotAssigned > </PRE ></FONT ></TD ></TR ></TABLE ><P >A variable can be referred to as <TT CLASS="VARNAME" >$bar</TT > or <TT CLASS="VARNAME" >${bar}</TT >. The braces are useful when it is unclear what is being referenced: if I write <TT CLASS="VARNAME" >$barley</TT > do I mean <TT CLASS="VARNAME" >${bar}ley</TT > or <TT CLASS="VARNAME" >${barley}</TT >? Note also that referencing a value that hasn't been assigned doesn't generate an error, instead returning nothing. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="c189.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x206.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Bash Programming and Shell Scripts</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c189.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Quotes and Special Characters</TD ></TR ></TABLE ></DIV ></BODY ></HTML >