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

Variable introduction of shell in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The editor in this issue will bring you an introduction to the variables of shell in Linux, and analyze and describe them from a professional point of view. I hope you can get something after reading this article.

We know that there are many variables in shell, such as the PATH variable that we often use, and its function is to set the directory of the executable file so that you don't have to use an absolute path when entering commands. In addition, shell has many other variables. Today, let's talk about the shell variable.

classification

As we know, there is a distinction between local variables and global variables in php. Shell is very different from php, and there are environment variables and ordinary variables. Environment variables are generally used to define the environment in which shell is run, while ordinary variables are often used in writing shell scripts.

The difference between environment variables and ordinary variables is very similar to php variables: the difference is in the scope of use, shell environment variables can be used in the current shell and derived shell, while ordinary variables can only be used in the current shell. Environment variables are usually uppercase, and ordinary variables are usually lowercase.

Define variable

When defined, 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).

Environment variable

To view all the current environment variables, use the env (environment) command

# envXDG_SESSION_ID=38135HOSTNAME=iz8vb626ci0aehwsivxaydzTERM=xtermSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=114.106.186.229 31955 22QTDIR=/usr/lib64/qt-3.3.

You can also view it with the set command, but this command lists all environment variables as well as normal variables. In general, environment variables are represented by uppercase letters.

Set and cancel environment variables

Users can customize the way to set environment variables as follows

Export variable name = variable value

If you want to cancel the environment variable, you can use the unset command to do so

Unset variable name

Here's a demonstration:

# export HOBBY=basketball# env | grep HOBBYHOBBY=basketball# unset HOBBY# env | grep HOBBY

Printing and setting of variables

There is an echo command under linux, using the same method as php, which is used to print a piece of text.

# echo hello,worldhello,world# echo-e "$PWD\ n$PATH" / root/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

Rules for setting variables

Variable name = variable content

Note that there can be no spaces on either side of the "="

Variable names can only be letters and numbers, and the first character cannot be a number

If the variable content has spaces, you need to use quotation marks (single quotation marks, double quotation marks) to widen the variable content, such as name= "lebron james". Variables can be parsed in double quotes.

If you have special characters, you can use\ to escape them, such as enter,', ", etc.

If you want to use the result of the command as the content of a variable, you can use $(command) or command

If you want to expand the content of a variable, you can use "{$variable} accumulate content"

Array

Like php, shell variables have array types. The definition syntax of the array is as follows:

Variable name = (val1 val2... Valn); arr= (paul james durant)

Print the length of the array

Echo ${# arr [@]}

Print the length of the array elements

Echo ${# arr [array subscript]}

Print some elements of the array ${lnmp [@]: n1:n2} N1 for start, N2 for length

# print all elements # echo ${lnmp [@]} linux nginx mysql php# echo ${lnmp [@]: 0} linux nginx mysql php# print all values starting from the second element # echo ${lnmp [@]: 1} nginx mysql php# print the first and second values # echo ${lnmp [@]: 0:2} linux nginx # print the second and third values # echo ${lnmp [@]: 1:2} nginx mysql

The syntax for printing array elements is as follows

Echo ${arr [element subscript]} # echo ${arr [0]} paul# echo ${arr [1]} james

Print all the elements of the array

Echo ${arr [@]}

Assignment, replacement, deletion of array

# append element # lnmp [${# lnmp [@]}] = apache# echo ${lnmp [@]} linux nginx mysql php apache# modify element # lnmp [0] = 1 # echo ${lnmp [@]} linux nginx mysql php # Delete element unset lnmp [0] unset lnmp [1] unset lnmp above is the introduction of shell variables in Linux shared by Xiaobian. If you have similar doubts, it does not hinder understanding with reference to the above analysis. If you want to know more about it, please follow the industry information.

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