![]() |
A variable is a basic building block that stores a value at a specific memory location, and every operation performed on a variable affects that memory location. Values stored in variables can be changed or deleted during program execution. The main difference with bash variables is that they don’t need to be declared. Just assign the value of the variable using the variable name. In Linux bash commands and scripts, there are two different ways to define variables:
What is Shell Variable?Shell variables are local variables whose value we can only access within that shell. There are many uses for shell variables, some of which are listed below:
Syntax for declaring Shell Variable:Variable_Name = initialValue Example:message="Hello from GeeksforGeeks" ![]()
What is Environmental Variable?Environment variables are variables whose values are set globally in a program and can be inherited by subshells, processes, and commands. Environment variables have many uses, some of which are listed below:
Syntax for declaring Environmental Variable:export Variable_Name = initial value Example:export Message="Hello from GeeksforGeeks" ![]()
The main difference between the two is that the export command makes the variable available to all subsequent commands run in that shell. Relationship Between Parent Shell and Sub Shell
More on Export CommandExporting Bash FunctionsThe Export command can also be used to export bash functions. Step 1: Create a Bash Function function message{ echo "Hello from GeeksforGeeks" } ![]()
Step 2: We can use the export -f command line option to export functions so that they are also available in subshells and processes: export -f message The function is now available in sub-shells. ![]()
Removing the value of an Exported VariableFor removing the value of an exported variable we can use the unset command which is in-built bash command that can be used to delete the value of an exported variable. Syntaxunset Environmental_Variable_Name Exampleunset message ![]()
Viewing All Exported VariablesStep 1: If the export command takes no arguments, it displays a list of all variables. To export all listed variables to the subprocess, use the -p option. export -p ![]()
Step 2: With the help of the below command, we can undo the effects of exporting -p command. export -n The variable is again restricted to the current shell session. More on Bash Script and ExportIn order to get the expected results with the export command when creating a bash script, we need to keep the following points in mind: Export when running a bash scriptSuppose you run a bash script in its own subshell inside a bash command that contains an export command. So the variables exported from the script are only used in the subshell, not the parent shell. Step 1: You can easily create a file to store your script (“outscript.sh”) with the following command: echo "export message='Hello from GeeksForGeeks'" > outscript.sh ![]()
Step 2: With the help of “chmod +x”, we can load “outscript.sh” into bash and run it. chmod +x outscript.sh Exported variables disappear from the environment. ![]()
Source Variables From a Bash Script to the Current ShellAs shown above, we know that the bash script will never export its exported variables back to the calling shell, but the bash script also provides us with a command that can handle this situation easily. The source command allows us to load variables and functions into the bash shell and use them in the current shell without creating a subshell. Step 1: You can easily create a file to store your script (“outscript.sh”) with the following command: echo "export message='Hello from GeeksForGeeks'" > outscript.sh Step 2: With the help of “source”, we can load “outscript.sh” into bash and run it. source outscript.h ![]()
|
Reffered: https://www.geeksforgeeks.org
How To |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |