You are currently viewing Shell Scripting – Local Variables
Shell Scripting – Local Variables

Shell Scripting – Local Variables

Shell Scripting – Local Variables

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed special characters in the shell.

https://cloudaffaire.com/shell-scripting-special-characters/

In this blog post, we will discuss variables in the shell. Like any other scripting language, the shell also supports variables. A variable, in a nutshell, is a temporary space to store some value. In the shell, you can store either string or integer as a variable value. Variables can be user-defined or system-defined and the scope of the variables can be local or global. In this blog post, we will mainly focus on user-defined local variables.

Shell Scripting – Local Variables

Variable declaration:

There are several ways you can define a variable in the shell but the preferred way is by using declare statement. Variable can also be assigned from user input, from command output, from a file, or from the value of another variable.

Variable substitution:

You can substitute a variable value at any time.

Variable types:

Shell only supports the variable of type string or integer. But that does not mean you can not use other types of variables like float or Boolean.

Variable scope:

A local variable is bound with the process that has initialized it. Another process can not view or use the value of a local variable. You can use the export command to make a local variable global. A global variable value can be accessed by any process. You can also use the local command to explicitly define a local variable (available within functions only).

Hope you have enjoyed this article. In the next blog post, we will discuss user-defined global variables.

 

Leave a Reply