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

How to learn Shell variables

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to learn Shell variables, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Shell is a program written in C language, which is a bridge for users to use Linux. Shell is both a command language and a programming language.

Shell is an application that provides an interface through which users access the services of the operating system kernel.

When defining a variable, the variable name is not marked with a dollar sign ($, which is required in the PHP language), such as:

Your_name= "runoob.com"

Note that there can be no spaces between the variable name and the equal sign, which may be different from all the programming languages you are familiar with. At the same time, the naming of variable names must follow the following rules:

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).

Examples of valid Shell variable names are as follows:

RUNOOBLD_LIBRARY_PATH_varvar2

Invalid variable name:

? var=123user*name=runoob

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

For file in `ls / etc` or for file in $(ls / etc)

The above statement loops out the file name of the directory under / etc.

Use variables

To use a defined variable, simply precede the variable name with a dollar sign, such as:

Your_name= "qinjx" echo $your_nameecho ${your_name}

The curly braces outside the variable name are optional, whether to add them or not, and the curly braces are added to help the interpreter identify the boundaries of the variable, as in the following case:

For skill in Ada Coffe Action Java; doecho "I am good at ${skill} Script" done

If you don't add curly braces to the skill variable and write it as echo "I am good at $skillScript", the interpreter will treat $skillScript as a variable (its value is empty), and the code execution result will not be what we expected.

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

Defined variables can be redefined, such as:

Your_name= "tom" echo $your_nameyour_name= "alibaba" echo $your_name

It's legal to write this way, but note that you can't write $your_name= "alibaba" on the second assignment, but only add the dollar sign ($) when using variables.

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.

The following example attempts to change a read-only variable and reports an error:

#! / bin/bashmyUrl= "http://www.google.com"readonly myUrlmyUrl=" http://www.runoob.com"

Run the script and the result is as follows:

/ bin/sh: NAME: This variable is read only.

Delete a variable

Use the unset command to delete a variable. Syntax:

Unset variable_name

The variable cannot be used again after it has been deleted. The unset command cannot delete read-only variables.

Example

#! / bin/shmyUrl= "http://www.runoob.com"unset myUrlecho $myUrl

The execution of the above example will have no output.

Variable type

When running shell, three variables exist at the same time:

1) Local variables are defined in scripts or commands and are only valid in the current shell instance. Other programs started by shell cannot access local variables.

2) Environment variables all programs, including those started by shell, can access environment variables, and some programs need environment variables to ensure their normal operation. Shell scripts can also define environment variables if necessary.

3) shell variable shell variable is a special variable set by the shell program. Some of the shell variables are environment variables and some are local variables. These variables ensure the normal operation of shell.

After reading the above, have you mastered how to learn Shell variables? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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