In this section, we will learn how to use Shell variables in UNIX. A variable is a nature string to which we assign a value. The value assigned could be a number, text, filename, device, or any other kind of information.
A variable is nothing more than a pointer to the concrete information. The shell enables you to create, assign, and delete variables.
The name of a variable can contain only letters (a to z or A to Z), numbers (0 to 9) or the underscore character (_).
By convention, UNIX shell variables will have their names in UPPERCASE.
The following illustration is suitable variable names –
Following are the illustration of invalid variable names –
The reason you cannot use other characters such as! *, or - is that these font have a special importance for the shell.
Variables are defined as follows –
For instance –
The above instance defines the variable NAME and assigns the value "Zara Ali" to it. Variables of this kind are called scalar variables. A scalar variable can hold only one value at a time.
Shell enables you to store any value you want in a variable. For instance –
To access the value store in a variable, prefix its name with the dollar sign ($) −
For instance, the following script will contact the value of defined variable NAME and feature it on STDOUT –
The above script will create the following value –
Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be transformed.
For instance, the following script generate an error while trying to modify the value of NAME –
The above script will make the following result –
Unsettling or deleting a variable directs the shell to eliminate the variable from the list of variables that it tracks. Once you unset a variable, you cannot contact the store value in the variable.
Following is the syntax to unset a define variable using the unset command –
The above command unseats the value of a define variable. Here is a simple instance that demonstrate how the command works –
The above instance does not print anything. You cannot use the unset command to unset variables that are distinct read-only.
When a shell is management, three most important types of variables are present −
Unix/Linux Related Interview Questions
|Perl Scripting Interview Questions||Shell Scripting Interview Questions|
|Python Interview Questions||Core Java Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|Red Hat Linux System Administration Interview Questions||IBM AIX Interview Questions|
|Sybase Interview Questions||IBM AIX 7 Administration Interview Questions|
|Unix Shell Scripting Interview Questions||Linux Shell Scripting Interview Questions|
Unix/Linux Related Practice Tests
|Perl Scripting Practice Tests||Shell Scripting Practice Tests|
|Python Practice Tests||Core Java Practice Tests|
|Linux Practice Tests||Linux Embedded systems Practice Tests|
|Red Hat Linux System Administration Practice Tests||IBM AIX Practice Tests|
|Sybase Practice Tests||IBM AIX 7 Administration Practice Tests|
|Autosys Practice Tests|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.