Unix How Do You Read Values From File Into a Variable
Unix / Linux - Using Shell Variables
In this chapter, we will learn how to utilize Shell variables in Unix. A variable is a grapheme string to which we assign a value. The value assigned could be a number, text, filename, device, or any other blazon of information.
A variable is aught more than a pointer to the actual information. The shell enables you to create, assign, and delete variables.
Variable Names
The proper noun of a variable tin can contain but letters (a to z or A to Z), numbers ( 0 to 9) or the underscore grapheme ( _).
Past convention, Unix shell variables volition have their names in UPPERCASE.
The post-obit 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 you cannot use other characters such as !, *, or - is that these characters accept a special meaning for the shell.
Defining Variables
Variables are divers as follows −
variable_name=variable_value
For example −
NAME="Zara Ali"
The to a higher place example defines the variable NAME and assigns the value "Zara Ali" to it. Variables of this type are called scalar variables. A scalar variable tin hold just one value at a time.
Shell enables you to store whatsoever value yous want in a variable. For example −
VAR1="Zara Ali" VAR2=100
Accessing Values
To access the value stored in a variable, prefix its proper noun with the dollar sign ($) −
For example, the post-obit script will access the value of defined variable Name and print information technology on STDOUT −
#!/bin/sh Proper name="Zara Ali" repeat $NAME
The above script will produce the post-obit value −
Zara Ali
Read-but Variables
Vanquish provides a way to marking variables as read-only by using the read-only control. After a variable is marked read-merely, its value cannot be inverse.
For case, the following script generates an mistake while trying to modify the value of Proper name −
#!/bin/sh Name="Zara Ali" readonly NAME NAME="Qadiri"
The above script will generate the following result −
/bin/sh: Proper name: This variable is read merely.
Unsetting Variables
Unsetting or deleting a variable directs the shell to remove the variable from the listing of variables that it tracks. Once you unset a variable, yous cannot access the stored value in the variable.
Following is the syntax to unset a defined variable using the unset control −
unset variable_name
The above command unsets the value of a defined variable. Hither is a simple example that demonstrates how the command works −
#!/bin/sh NAME="Zara Ali" unset NAME echo $NAME
The above case does not print anything. You cannot use the unset command to unset variables that are marked readonly.
Variable Types
When a crush is running, three main types of variables are present −
-
Local Variables − A local variable is a variable that is present within the electric current instance of the shell. It is not bachelor to programs that are started past the shell. They are set at the command prompt.
-
Environment Variables − An environment variable is available to whatever kid process of the vanquish. Some programs need environment variables in lodge to function correctly. Unremarkably, a shell script defines only those environment variables that are needed by the programs that it runs.
-
Trounce Variables − A shell variable is a special variable that is fix past the beat and is required by the beat in lodge to function correctly. Some of these variables are environment variables whereas others are local variables.
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/unix/unix-using-variables.htm
0 Response to "Unix How Do You Read Values From File Into a Variable"
إرسال تعليق