Understanding variables

Let's learn about creating variables in a shell.

Declaring variables in Linux is very easy. We just need to use the variable name and initialize it with the required content.

$ person="Ganesh Naik"

To get the content of the variable, we need to add the prefix $ before the variable, for example:

    $ echo person        person
    $ echo $person
    Ganesh Naik

The unset command can be used to delete the declared variable:

$ a=20$ echo $a$ unset a

The unset command will clear or remove the variable from the shell environment as well.

Here, the set command will show all variables declared in the shell:

$ person="Ganesh Naik"$ echo $person$ set

Here, using the declare command with the -x option will make it an environmental or global ...

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.