Changes

Jump to: navigation, search

BASH Variables

592 bytes added, 22:17, 21 September 2008
no edit summary
[[BASH]] supports one type of variable: a string. Variables may be interpreted as integers or booleans (True/False) when appropriate.
= Creating or Assigning a Variable =
COLOUR="Red"
NAME="Jason Smith"
EMAIL="jsmith@example.com>"
Variable names must start with a letter and contain only letters, underscores, and digits. Variable names are case-sensitive, and UPPERCASE is often used to make it easy to distinguish between variable names and commands and arguments.
echo "Hooray!"
fi
 
Variables are interpolated when in double-quotes, but not when they are in single-quotes:
 
$ X="Test"
$ echo "$X"
Test
$ echo '$X'
$X
$ echo $X
Test
 
One advantage to using double-quotes is that the variable value will be treated as a single argument even when it contain spaces. For example:
 
$ touch "test file"
$ NAME="test file"
$ rm $NAME
rm: cannot remove `test': No such file or directory
rm: cannot remove `file': No such file or directory
$ rm "$NAME"
You may optionally place the variable name within curly-braces (useful if there is text immediately after the variable name):
|-
|$RANDOM
|Random integer(usually in the range 0-327687).
|}

Navigation menu