Linux Series Tutorials (22)--linux bash variables

Source: Internet
Author: User

On the previous blog we introduced some of the basic features of bash, which we usually operate most frequently. In this blog we introduce the variables of bash, laying the groundwork for scripting shell scripts later.

1. What is a variable

A variable is a unit of computer memory where the stored value can be changed.

When a shell script needs to save some information, such as a file name or a number, put it in a variable. Each variable has a name that can be used to refer to the variable by name.

Variables can be used to save useful information so that the system learns about the user's settings, and variables can also hold temporary information.

2. Declaration rules for variables

  

  

  

3. Classification of variables

  

4. Usage of user-defined variables

A user-defined variable is also a local variable. Takes effect only in the current shell.

①, variable definition
Variable name = variable Value

  

②, variable invocation
$ variable Name

  

③, variable view
Set

Set is to view all variables defined in the current system.

  

④, variable deletion
unset variable Name

5. Usage of environment variables

Environment variables mainly hold variables related to the operating environment of the system.

The previously mentioned user-defined variable (local variable) takes effect only in the current shell, and the environment variable takes effect in the current shell and all child shells of the shell, and if the environment variable is written to the appropriate configuration file, the environment variable will take effect in all shells.

①, Pstree command to distinguish whether the level of the current shell is a parent or a child

  

②, declaring environment variables
Export variable name = variable Value
③, querying all environment variables
Env

The set command mentioned above is to view all variables, while Env is to view environment variables.

④, view, delete specified environment variables
$ variable name          #查看环境变量unset variable name   #删除环境变量

These two are the same as local variables.

  

⑤, the PATH environment variable for the system lookup command $PATH

  We know that the calling command must be in the current directory, or in an absolute path. But actually we call a command to use the command name directly, such as Cd,ls and so on, which is why?

The reason is that in the $PATH we have defined, we execute a command of a name, the system will first go to $path inside the search, if not found will not find the command error.

  First look at the $path:

  

As you can see, $PATH inside are some of the paths that are saved, and then by: Delimited.

We've written a hello.sh script before, and we know that to execute it, you must first give the executable permission and then either execute it through the./hello.sh in the current directory or through the absolute path/tmp/hello.sh. What if we want to do it directly through hello.sh?

The workaround is to add the path of the hello.sh to the $PATH variable, as follows:

  

⑥, defining the system prompt variable $PS 1

  

  

6. Usage of positional parameter variables

  

  

7. Usage of predefined variables

In fact, the predefined variable is also a positional parameter variable, there are several uses:

  

8. Declaring variable type declare

  

9. Three methods of numerical operation ①, Declare-i

  

②, expr, or let numeric arithmetic tools

  

③, $ (expression), or $[expression]

  

10. Operators and their order of precedence

  

11. Variable Test and replacement

  

12. Environment variable configuration file

Before we put the environment variable, we added the path of the script to the $PATH path, and then we could execute the script directly from the script's name. However, if the system restarts, the discovery must be re-added to the $PATH variable. So is there a way to keep it in effect?

The workaround is to include the path to the script in the environment variable configuration file.

The environment variable configuration file primarily defines the system default environment variables that are in effect for the operating environment of the system, such as the default environment variables such as Path,histsize,ps1,hostname. The following configuration files are available:

  

is the order in which the environment variable files are read: (The following configuration file is re-read each time the user logs back in)

  

We know $PATH variables, each time the user logs in, it reads all the configuration files from the file read order above, and finally gets the value of the $PATH variable.

Note: The more you go back to the configuration file, the higher the content priority of the variable that is configured inside.

  

  

  

  

    

Linux Series Tutorials (22)--linux bash variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.