Variables

Bash Prompt HOWTO:
Prev | Chapter 3. Bash Programming and Shell Scripts | Next

# 3.1. Variables

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 Learning the Bash Shell 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.

Variables in Bash are assigned much as they are in any programming language:

    testvar=5
    foo=zen
    bar="bash prompt"

Quotes are only needed in an assignment if a space (or special character, discussed shortly) is a part of the variable.

Variables are referenced slightly differently than they are assigned:

    > echo $testvar
    5
    > echo $foo
    zen
    > echo ${bar}
    bash prompt
    > echo $NotAssigned

    >

A variable can be referred to as `$bar` or `${bar}`. The braces are useful when it is unclear what is being referenced: if I write `$barley` do I mean `${bar}ley` or `${barley}`? Note also that referencing a value that hasn't been assigned doesn't generate an error, instead returning nothing.

Prev | Home | Next
Bash Programming and Shell Scripts | Up | Quotes and Special Characters