Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


Variables



In bash all variables are STRINGS by default
There are NO data types
Variables are CASE sensitive
No spaces around '=' in assignment

Every programming language allows the use of variables, which we know are place-holders for values.

To create a variable, just use its name and assign it a value:

myName=Fubar
yourName=Julienne
echo Hello $myName! What up?
Hello Fubar! What up?
myName = Bufar
-bash: myName: command not found

That produced the error message because we had spaces around the '=' character.

It's considered good practice to use lowercase variable names,
as uppercase ones are used by the shell and you run the risk of clashing with them.

When we need to refer to or use a variable, we need to prefix it with a dollar sign ($)

A useful thing we can do with variables, is assign the result of a system command to one.

This is a bash expansion known as command substitution.

myName=Fubar
today=$(date) # assign result of the 'date' command to a variable
echo Hello $yourName! My name is $myName on $today
Hello Julienne! My name is Fubar on Thu Dec 19 12:57:50 EST 2024