Bash variables

In bash in order to set a variable, you can just use the equal sing ‘=’.So you write the variable name, equal sign and it value.
It is important not to have a space between the variable name and the equal sing.
When we want to access the variable then we use dollar sign ‘$’.

For example to define username as “bob”:


[bob@localhost ~]$ username="bob"
[bob@localhost ~]$ echo $username 
bob

So as we see we define username variable to have the value “bob”, and then we echo the username varibles and got bob.

However it is important to know thant when we set varibales they are not accessiable for our subshell (unless we export them). So if we create script that echo the username varible, then this script will show nothing, bacuse it doen’t know about the varible username.
Let see the following example:


[bob@localhost ~]$ username="bob"
[bob@localhost ~]$ echo $username 
bob
[bob@localhost ~]$ vim showusername.sh
[bob@localhost ~]$ cat showusername.sh 
#!/bin/bash
echo "begin"
echo "$username"
echo "end"
[bob@localhost ~]$ chmod +x showusername.sh 
[bob@localhost ~]$ ./showusername.sh 
begin

end
[bob@localhost ~]$ 

As we can see just like before, we have set a variable called username to the value of “bob”. we then created a script that echo the word begin, the value of the variable username, and then the word end. When we run this script, we see that instead of printing:

begin
bob
end

We got:

begin

end

the variable is not accessible from the script because the script is not the same shell, but a sub shell.
If we want to make variable accessible to all subshell of  a shell, then we can use export command.
For example:


[bob@localhost ~]$ username="bob"
[bob@localhost ~]$ echo $username 
bob
[bob@localhost ~]$ vim showusername.sh
[bob@localhost ~]$ cat showusername.sh 
#!/bin/bash
echo "begin"
echo "$username"
echo "end"
[bob@localhost ~]$ chmod +x showusername.sh 
[bob@localhost ~]$ ./showusername.sh
[bob@localhost ~]$ export username 
[bob@localhost ~]$ ./showusername.sh 
begin
bob
end

Now we have exported the variable, so the script can access it. we did it by using the command export.
So as we can see, now it does print the word “bob”.

if we want all our variable to be automatically exported, we can use the set -o allexport command.
For example:


[bob@localhost ~]$ set -o allexport 

This will make all our variables, automatically exported when created (meaning accessible for subshell).
If we want script or a program to be able to change the variables of the current shell, then we need to use the command source, otherwise the script will mask our variables, but wont change our variables.
For example:


[bob@localhost ~]$ cat showusername.sh 
#!/bin/bash
echo "begin"
echo "$username"
echo "end"
username="julia"
echo "begin"
echo "$username"
echo "end"
[bob@localhost ~]$ echo "$username"
bob
[bob@localhost ~]$ ./showusername.sh 
begin
bob
end
begin
julia
end
[bob@localhost ~]$ echo $username 
bob
[bob@localhost ~]$ echo "$username"
bob

As we can see, we have indeed change the variable username to julia, and during the script running, it got the value and print it (print julila).However once the script has finished, we are back with the value “bob”.
But we can use the source command, and in such case, it will change the value of the variable username in original shell.
Lets see:


[bob@localhost ~]$ source showusername.sh 
begin
bob
end
begin
julia
end
[bob@localhost ~]$ echo $username 
julia

So in this example, we have used the source command, and has we can see it was able to change the variable in the shell that execute the script.
So if we want the script or program to change the variable in our shell the we should use the source command.

if we want to delete/unset a variable, then we can simply use the unset command.
For example:


[bob@localhost ~]$ fruit=apple
[bob@localhost ~]$ echo $fruit 
apple
[bob@localhost ~]$ unset fruit 
[bob@localhost ~]$ echo $fruit 

[bob@localhost ~]$ 

As we can see, we set the variable fruit to be apple, and indeed when we echoed it, then we get apple.
But after we used the unset command, when we echoed itwe got nothing, because it was already unset.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s