UNIX / Linux Using Shell Variables - Unix/Linux

What are UNIX / Linux Using Shell Variables?

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.

Variable Names

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.

Defining Variables

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 –

Accessing Values

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 –

Read-only Variables

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 Variables

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.

Variable Types

When a shell is management, three most important types of variables are present −

  • Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
  • Environment Variables − an environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.
  • Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics