How To Set Environment Variable In Linux Using Shell Script
Unix / Linux - Using Vanquish Variables
In this chapter, we will learn how to utilise Shell variables in Unix. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or whatever other type of data.
A variable is zippo more than a pointer to the actual data. The trounce enables you to create, assign, and delete variables.
Variable Names
The proper noun of a variable can contain merely letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
By convention, Unix shell variables will accept their names in Uppercase.
The following examples are valid variable names −
_ALI TOKEN_A VAR_1 VAR_2
Following are the examples of invalid variable names −
2_VAR -VARIABLE VAR1-VAR2 VAR_A!
The reason yous cannot utilise other characters such equally !, *, or - is that these characters have a special meaning for the shell.
Defining Variables
Variables are defined as follows −
variable_name=variable_value
For case −
NAME="Zara Ali"
The above case defines the variable NAME and assigns the value "Zara Ali" to it. Variables of this type are called scalar variables. A scalar variable tin can hold merely one value at a fourth dimension.
Shell enables you to store whatsoever value you desire in a variable. For example −
VAR1="Zara Ali" VAR2=100
Accessing Values
To access the value stored in a variable, prefix its name with the dollar sign ($) −
For example, the post-obit script will access the value of defined variable NAME and print it on STDOUT −
#!/bin/sh NAME="Zara Ali" echo $Proper noun
The higher up script will produce the following value −
Zara Ali
Read-only Variables
Shell provides a way to mark variables every bit read-only by using the read-but command. Later on a variable is marked read-only, its value cannot be changed.
For case, the following script generates an error while trying to alter the value of NAME −
#!/bin/sh NAME="Zara Ali" readonly Proper name Proper noun="Qadiri"
The to a higher place script volition generate the post-obit result −
/bin/sh: NAME: This variable is read just.
Unsetting Variables
Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable.
Post-obit is the syntax to unset a defined variable using the unset command −
unset variable_name
The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the command works −
#!/bin/sh Proper noun="Zara Ali" unset NAME repeat $NAME
The to a higher place case does not print anything. You cannot use the unset command to unset variables that are marked readonly.
Variable Types
When a shell is running, three main types of variables are present −
-
Local Variables − A local variable is a variable that is nowadays within the electric current instance of the shell. Information technology is not available to programs that are started by the vanquish. They are set at the control prompt.
-
Environment Variables − An surround variable is available to any child procedure of the crush. Some programs need environment variables in club to function correctly. Usually, a vanquish script defines just those environment variables that are needed by the programs that it runs.
-
Beat Variables − A trounce variable is a special variable that is set by the shell and is required past the shell in order to part correctly. Some of these variables are environment variables whereas others are local variables.
Useful Video Courses
Video
Video
Video
Video
Video
Video
How To Set Environment Variable In Linux Using Shell Script,
Source: https://www.tutorialspoint.com/unix/unix-using-variables.htm
Posted by: johnsacurnhooks1950.blogspot.com
0 Response to "How To Set Environment Variable In Linux Using Shell Script"
Post a Comment