Variables - bash
var_name="string"
In bash - NO spaces between variable name, equal, and string being
assigned.
If the string contains spaces, it must be quoted.
To access the contents, reference with $
echo $var_name
Variable names
Alpha, numeric, underscore "_"
First character cannot be numeric.
Case sensitive.
Two variable scopes.
local - normal status. Not exported to commands or child shells.
global, exported, or environmental
- exported to commands or child shells. Don't have to be used.
- Environmental variables usually capped, but not a requirement.
- Several already assigned.
set - lists all variables, aliases, and functions
defined in current shell.
env - lists variables with an exportable status.
Two types.
scalar - normal variable contains one value.
array - bash can create variable arrays.
Variables can be assigned contents of other variables.
var1="house"
echo $var1
house
var2="boat$var1"
echo $var2
boathouse
var3="$var1boat"
echo $var3
# the variable $var1boat doesn't exist so a null was assigned to $var3
# we can clarify what we mean with braces.
var3="${var1}boat"
echo $var3
houseboat
#Use space to separate variable and string but requires quotes
# and will have a space.
var3="$var1 boat"
echo $var3
house boat
#Assign all strings to variables and concatenate.
var3="boat"
var4="$var1$var3"
echo $var4
houseboat
Reassignment and removing variables.
To overwrite variable contents, just assign new value.
In bash, you can unset a variable. Don't use $.
unset var3
readonly
To make a variable readonly, use :
readonly varname
Targeting variable. Don't use $.
Make sure you have already assigned it a value.
readonly will protect the variable from being changed or removed
in current shell. You cannot unset the variable.
Quoting
There will be a number of reasons to quote strings and variable names.
depending on the situation.
Quotes
Exporting
Variables are local to shell or shell script in which they are created.
They can be marked exportable and will be exported to any child process.
greet="Hi there"
export greet
export greeting="Hey there"
readonly status is not exported.
So, variable exported to child can be modified or unset.
However, on returning to parent process, original value still exists.
You cannot pass new value back to parent using the variable.
array variables
To create an array variable, use commands declare or typeset
- Required.
Some options :
- -a create an indexed array
- -A associative variable.
- -r readonly. Once declared, cannot be changed or unset.
- -x make exportable.
The export command won't work.
declare -a ary="( red green blue )"
echo $ary
red
# returns 1st element.
echo $ary[1]
red[1]
# returns 1st element with the [1] appended.
# use {} to indicate ary[1] is what is being referenced.
echo ${ary[1]}
green
# change contents of target element.
ary[1]=brown
echo ${ary[1]}
brown
Indexed from 0.
Braces, {}, required when referencing.
Quoted strings treated as single element.
declare -a ary='( red "light green" blue )'
echo ${ary[1]}
light green
Use index value to assign individual target element :
ary[5]="spring green"
echo ${ary[5]}
spring green
Elements don't have to be assigned sequentially.
Contents of an array can be dumped with :
echo ${ary[*]}
In this form, you will not be able to determine spaces in an array element.
declare -A Ary
Ary[one]=won; Ary[two]=to; Ary[three]=tree; Ary[four]=fore
or
declare -A Ary=( [one]=won [two]=to [three]=tree )
Ary+=([five]=hive [six]=nix [seven]=heaven )
The += appends to existing array. Without it, new version of array created.
Also works with the indexed array.
echo ${Ary[*]}
heaven won hive nix to tree
# Note internal ordering is done via a hash and order unpredictable.
echo ${Ary[one]}
won
Do a google search on bash associative array for more info.
read built-in.
Interactive way of assigning values to variables.
A Practical Guide to Linux pp. 477-479, 479-480
Use read to get input from user and assign it to a variable.
# -n suppresses line feed on the echo
# reads input from keyboard until [cr] and assign to variable food.
echo -n "Feed me : ";read food
# Now echo out food
echo $food
# -p prompt #
# One string per variable using white-space as delimiter.
# if short, extra variables not used.
# if too many strings, left overs put in last variable.
read -p "I want more : " food1 food2 food3
echo $food1
echo $food2
echo $food3
|
- Multiple variables allowed.
- Uses IFS to separate input for variable assignment.
- Does not recognize quoted strings as single argument.
- But backslash can be used to denature space.
- Or reassigning IFS to different characters
- Assigns one string per variable.
- Last variable receives all 'left over' words/strings.
- Unused variables created but assigned null.