Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell variable

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Variable basis

In Bash shell, the value of each variable is a string, and when a variable is assigned, the value is stored as a string. That is, Bash shell does not distinguish between variable types by default, and even if you assign integers and decimals to variables, they are treated as strings.

Variable name

Naming can only use letters, numbers and underscores, and the first character cannot start with a number.

There can be no spaces in the middle, you can use an underscore (_).

You cannot use punctuation.

You cannot use keywords in bash (you can view reserved keywords with the help command).

In addition to explicit direct assignment, you can also use sentences to assign values to variables

Use variables

To use a defined variable, simply precede the variable name with the dollar sign $

It is recommended to add curly braces {} to all variables, which is a good programming habit.

Example:

Modify variable value

When assigning a value to a variable the second time, it cannot be added before the variable name, only when the variable is used, only when the variable is used, and only when the variable is used.

Example:

The difference between single quotation marks and double quotes

When defining a variable, the value of the variable can be enclosed in single or double quotation marks

Example:

When you surround the value of a variable in single quotation marks, you output whatever is inside the single quotation mark, even if there are variables and commands in the content (the command needs to be reversed). This approach is more suitable for defining situations where pure strings are displayed, that is, scenarios where you do not want to parse variables, commands, and so on.

When you surround the value of a variable in double quotes, the variables and commands in it will be parsed first instead of outputting the variable names and commands in double quotes as they are. This approach is more suitable for variable definitions that come with variables and commands in a string and want to parse them before outputting them.

Read-only variable

Using the readonly command, you can define a variable as a read-only variable, and the value of a read-only variable cannot be changed.

Variable scope Scope

The scope (Scope) of the Shell variable is the valid scope of the Shell variable (the range that can be used).

The scope of the Shell variable can be divided into three types:

Some variables can only be used within functions. This is called local variable.

Some variables can be used in the current Shell process, which is called global variable (global variable)

Some variables can also be used in child processes, which is called environment variable.

The form of export an is to export the variable an as an environment variable after defining it. If you want to export it as an environment variable at the same time, you can write export axiom 20.

Environmental variables are also temporary.

When you create a new Shell window, the environment variable is still not accessible on the second visit.

The environment variables exported through export are only valid for the current Shell process and all child processes. If the top-level parent process is shut down, the environment variables will disappear and other processes will not be able to use them, so the environment variables are temporary.

Command replacement

Shell command substitution refers to assigning the output of a command to a variable. For example, enter the ls command in a directory to view all the files in the current directory, but how do you store the output in a variable? This requires command substitution, which is a frequently used feature in Shell programming.

Replacement mode

There are two ways to complete command substitution in Shell, one is backquotation mark, the other is $()

Example

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report