Recent Posts

ads

Shell Scripting 2- Linux variables



A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.

A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.
Just like programming languages, Linux shell has the capability of storing data in variables. A variable is a container that temporarily stores data that will be processed through a programming language.
 There are two types of variables in Linux: the environment variables and shell 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.

Rules for environment variable
  • You cannot use other characters such as !, *, or - is that these characters have a special meaning for the shell.
  • The names of variables must begin with a letter or an underscore.
  • Variables are case-sensitive thus, the variables path, PATH and Path are different.
  • It must only contain alphanumeric characters or an underscore.
Examples are valid variable names :

_VICTOR
KEY_A
VAR_5
VAR_10

Examples of invalid variable names − :
5_VAR
-VARIABLE
VAR5-VAR10
VAR_C5!

Environment VariablesAn environment variable is available to any child process of the shell, and it 's the default variable in Linux. 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.The environment variables are the default variables in Linux and are used to pass information across processes in the shell. Environment variables are case-sensitive and should be always capitalized in order to access them.

The table below shows the common environment variables in Linux shell:

Variable name Usage
BASH          Holds the full path of the command interpreter for Bash scripts
BASH_VERSION Holds the bash release version of the machine currently used
HOME Holds the relative path of the home directory.
LOGNAME Holds the account name of the current user logged-in
OSTYPE Holds a string that describes the current OS of the machine used
PATH Holds a colon-separated absolute path of the executable files in Linux
PWD Holds the current working directory of the shell
SHELL Holds the preferred command line shell
USER Works similar to LOGNAME. It holds the account name of the user currently logged-in
_ Holds the name of the recently used command in the shell


 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.


We display the value of an environment variable, the user has to prepend a dollar-sign ($) to the variable to be accessed. So, to display some system information like the current working directory, logged-in user and the OS type and User  using echo we use:

#!/binbash
echo $PWD
echo $LOGNAME
echo $OSTYPE
echo $USER
So we go to the nano editor 


Now after been saved (ctrl + 0)
Back to the terminal, we will need to use chmod  to get permission

And we can get a list of environmental variables in Linux with ENV command 

To give more flexibility, the environmental variables can be manipulated so we will change values:
#!/bin/bash
echo $USER
USER="Superman"
echo $USER
So we will go to Nano text editor, and use the name "Superman"
We will save with the same name
We will run the script in our terminal

When we run the script, at first, the $USER in the line echo $USER shows the value of the USER variable. By using an assignment operator (=) the USER variable changes its value. 

The shell also allows the user to declare variables. To declare a variable in shell scripts, the user doesn't need to be concern about declaring its datatype; the interpreter will automatically detect the variable's datatype based on the data that the user stores into it during runtime.
Rules for shell Variables:
  1. The names of variables must begin with a letter or an underscore.
  2. It must only contain alphanumeric characters or an underscore.
  3. Variables are case-sensitive thus, the variables path, PATH and Path are different.

Let's check another variable in use in the terminal:

But we can find some issues when we want to put with other letters ahead of that variable... For that we will use some quotes

And maybe we can use variable to find out the number of files in Home directory



I hope that you learn about variables in Linux. I hope this lesson was useful for you. If you did learn something, please click like. Thanks in advance!
😄
Shell Scripting 2- Linux variables Shell Scripting 2- Linux variables Reviewed by ohhhvictor on December 07, 2018 Rating: 5

No comments:

Facebook

ads
Powered by Blogger.