Linux Bash How to Define, Use Shell Variables – POFTUT

Linux Bash How to Define, Use Shell Variables


[rps-include post=6835]

Variables are programming part of the bash and very useful. Without variables bash will be very inefficient shell. Variables are used by bash itself, running applications, daemons, X (graphic) server and for a lot of things.

Define Variable

Defining variables are easy in bash, just variable and data is enough. echo is used to print variables into terminal. With equal sign variable values are set. As you see page, Page, PAGE are different variables this means bash is case sensitive.

$ page="poftut.com" 
$ PAGE="http://poftut.com" 
$ Page="http://www.poftut.com" 
$ echo $page
poftut.com
$ echo $Page 
http://www.poftut.com
$ echo $PAGE 
http://poftut.com

Delete/Undefine Variable

Sometimes we don’t need variables that are created before and we want simple delete them. unset keyword is used to delete variable completely.

$ test=5
$ echo $test
5
$ unset test
$ echo $test

Change Variable Value

There is no extra effort to change existing variable value. It is just like creating new variable

$ page="poftut.com" 
$ echo $page 
poftut.com
$ page="www.poftut.com" 
$ echo $page
www.poftut.com

Concatenate Variables

To join variables together just put them together. In the second echo we put one space between hello and poftut variables which effects printed string too.

$ hello="Hello " 
$ poftut="www.poftut.com"
$ echo $hello$poftut 
Hello www.poftut.com
$ echo $hello" "$poftut 
Hello  www.poftut.com

We create two variables named hello and poftut. Than we echo both by joining together without any operator.In the second echo we put single space between them and then join all of them together.

[rps-include post=6835]

LEARN MORE  What Is Shebang Linux or Bash Term?

Leave a Comment