Variables (Shell Scripting)

Variables in shell scripts are very basic methods of storing information. They are always stored as strings, however different commands can treat them as numbers. Variables are created using the equal sign, and they are referenced by using a dollar sign.

#!/bin/bash

myvar=value
echo $myvar

A variable has scope within the process in which it is run. When the process is ended, its variables are deleted. In addition, when a sub process is started, the sub process will not have knowledge of any variables within its parent process. This can be changed however, by using the export command as follows:

#!/bin/bash

export myvar=value
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License