You can set your own environment variables directly from the bash shell.This section shows how to create your own environment variables and reference them from your interactive shell or shell script program.
Setting local environment variables
Once you start a bash shell (or spawn a shell script), you’re allowed to create local variables that are visible within your shell process. You can assign either a numeric or a string value to an environment variable by assigning the variable to a value using the equal sign:$ test=testing
That was simple! Now any time you need to reference the value of the test environment variable, just reference it by the name $test.
If you need to assign a string value that contains spaces, you’ll need to use a single quotation
mark to delineate the beginning and the end of the string:
Without the single quotation marks, the bash shell assumes that the next character is another command to process. Notice that for the local environment variable I defined, I used lower-case letters, while the system environment variables we’ve seen so far have all used upper-case letters.
This is a standard convention in the bash shell. If you create new environment variables, it is recommended (but not required) that you use lower-case letters. This helps distinguish your personal environment variables from the scores of system environment variables.
Once you set a local environment variable, it’s available for use anywhere within your shell process.
However, if you spawn another shell, it’s not available in the child shell:$ bash
In this example I started a child shell. As you can see, the test environment variable is not available in the child shell (it contains a blank value). After I exited the child shell and returned to the original shell, the local environment variable was still available.
Similarly, if you set a local environment variable in a child process, once you leave the child process the local environment variable is no longer available:$ bash
The test environment variable set in the child shell doesn’t exist when I go back to the parent shell.
Setting global environment variables
Global environment variables are visible from any child processes created by the process that sets the global environment variable. The method used to create a global environment variable is to create a local environment variable, then export it to the global environment.
This is done by using the export command:$ echo $test
After exporting the local environment variable test, I started a new shell process and viewed the value of the test environment variable. This time, the environment variable kept its value, as the export command made it global.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
Shell Scripting Tutorial
Starting With Linux Shells
Getting To The Shell
Basic Bash Shell Commands
More Bash Shell Commands
Using Linux Environment Variables
Basic Script Building
Understanding Linux File Permissions
Working With Editors
Using Structured Commands
More Structured Commands
Handling User Input
Adding Color To Scripts
Introducing Sed And Gawk
The Ash Shell
The Tcsh Shell
The Korn Shell
The Zsh Shell
Using The Web
Using A Database
Shell Script For Administrators
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.