Use variables in Shell

A variable is a string of characters from which we assign a value. The assigned value can be a number, text, file name, device or any other type of data.

A variable is nothing but a pointer to real data. Shell allows you to create, assign, and delete variables.

Variable names in Unix / Linux

The name of a variable can contain only characters (a to z or from A to Z), numbers (from 0 to 9), or underscores (_).

By convention, Unix variables will have their names in capital letters.

The following example is about a valid variable name.

 _ALI TOKEN_A VAR_1 VAR_2 

The following is an example of invalid variable names:

 2 _VAR - VARIABLE VAR1 - VAR2 VAR_A ! 

The reason is that you cannot use other characters like!, *, Or -, because these characters have special meaning for the shell.

Definition of variables in Unix / Linux

The variables are defined as follows:

 variable_name = variable_value 

For example:

 NAME = "Zara Ali" 

For example, define the Name variable and assign Zara Ali value to it. Variables of this type are called scala variables. A scalar variable can only hold one value at a time.

Shell allows you to keep any value you want in a variable. For example:

 VAR1 = "Zara Ali" VAR2 = 100 

Access values ​​in Unix / Linux

To access the values ​​held in a variable, precede the variable name with the symbol ($):

For example, the following script will access the value of the defined NAME variable and print it on STDOUT:

 #! / bin / sh NAME = "Zara Ali" echo $ NAME 

It will produce the following result:

 Zara Ali 

The variables are read-only (read-only) in Unix / Linux

The shell provides a way to mark variables as read-only using the readonly command. After a variable is read-only, its value cannot be changed.

For example, the following script will fail while trying to change the value of NAME variable.

 #! / bin / sh NAME = "Zara Ali" readonly NAME NAME = "Qadiri" 

It will produce the following result:

 / bin / sh : NAME : biến này được đọc chỉ . 

Delete a variable in Unix / Linux

Delete a variable for the shell to remove that variable from the list of variables it tracks. Once you delete a variable, you will not be able to access the value stored in that variable.

Here is the syntax to delete a defined variable using the unset command:

 unset variable_name 

The above command will delete the value of a defined variable. Here is a simple example:

 #! / bin / sh NAME = "Zara Ali" unset NAME echo $ NAME 

The above command will not print anything. You cannot use the unset command to delete variables that are read-only.

Variable types in Unix / Linux

When a Shell is running, there are three types of variables that exist:

Local Variables : An internal variable is a variable that exists in the shell's execution process. It is not available in programs that were started by Shell. They are set at the command prompt.

Environment Variables : An environment variable is a variable available in any shell subprocess. Some programs need environment variables to execute functions correctly. Usually a Shell script only defines the environment variables it needs by programs when it runs.

Shell Variables : A shell variable is a special variable that is set by Shell and is required by Shell to execute functions correctly. Some of these variables are environment variables, while others are internal variables.

According to Tutorialspoint

Previous article: What is Shell?

Next article: Special variables in Unix / Linux

4 ★ | 1 Vote

May be interested

  • REPL Terminal in Node.jsREPL Terminal in Node.js
    repl is an acronym for read eval print loop (understandably: reading - evaluating - printing - repeating) and it represents the computer environment like the console screen in the linux shell where you can type the command line and the system the system will return the results. node.js also has a repl environment.
  • Declare variables in SQL ServerDeclare variables in SQL Server
    sql server fully exists the concepts of data types, variables and declarations of variables as other programming languages. the article will learn how to declare a variable, multiple variables and assign default values ​​to variables in sql server.
  • How to create and run shell scripts in Ubuntu 22.04How to create and run shell scripts in Ubuntu 22.04
    shell is a user-written command interpreter. script shell helps users write and execute multiple commands at the same time. in this article, readers will learn how to execute shell scripts through command line input.
  • Global keywords in PythonGlobal keywords in Python
    what does the global key do and how do i use it in python? please follow us.
  • What is Linux Shell? The most popular Linux ShellsWhat is Linux Shell? The most popular Linux Shells
    are you satisfied using the bash shell in linux? or do you want to try an alternative? there are tsch, fish, kornshell and z shell for you to choose from. but which popular linux shell is best?
  • Nilesoft Shell - Menu customization application in Windows ExplorerNilesoft Shell - Menu customization application in Windows Explorer
    if you want more control, try nilesoft shell, an open source tool that lets you personalize the context menu to your liking.
  • Shell Sort in data structure and algorithmShell Sort in data structure and algorithm
    shell sort is a highly efficient sorting algorithm based on insertion sorting algorithm (insertion sort). this algorithm avoids the case of swapping positions of two distant elements in the selection algorithm (if the smaller element is in the right position quite far from the larger element on the left).
  • Variable scope in C ++Variable scope in C ++
    we will learn what functions and parameters of functions are in the next chapter. below we will explain the concept of local variables and global variables.
  • How to download and play Mortal Shell PC for free foreverHow to download and play Mortal Shell PC for free forever
    mortal shell is an action role-playing game with gameplay similar to the famous dark souls. to survive in mortal shell you need to defeat enemies quickly, accurately and without mercy. if you are planning to play mortal shell pc for free, quickly get the mortal shell game for free on the epic store right away.
  • Variable in JavaScriptVariable in JavaScript
    one of the most distinctive features of a program language is the set of data types it supports. this is the type of value that can be represented and manipulated in the program language.