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

Linux shell Foundation

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

Share

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

Blog structure

Know the shell of bash.

Variable function of Shell

one。 Know the shell of bash.

1. The operation of the computer

1. Hardware: of course, it is necessary for your hardware to be equipped with a "sound card chip", otherwise there will be no sound.

Core management: the core of the operating system can support this chipset, of course, it also needs to provide the driver of the chip; the application: requires the user (that is, you) to enter instructions to produce sound!

The main advantages of Bash are: 1. Command editing ability (~ / .bash_history) 2. Command and file completion function 3. Command alias setting function 4. Job control, foreground background control 5. Programmed script 6. Universal character

When using shell, variables are often used, and the definition of variables is very simple. You only need to meet the following three conditions:

The variable name consists of numbers, letters, and underscores

Must start with a letter or an underscore

You cannot use keywords in Shell, and the variable names in the following figure are legal.

What is a variable?

The simple definition is: a variable is a set of words or symbols, etc., instead of some settings or a string of retained data.

   1) variable and its contents are connected by an equal sign "=". Myname=VBird    2) the two sides of the equal sign cannot be directly connected with a space character. The following definition is incorrect:      myname=VBrid      myname=VBrid Tsai    3) the name of a variable can only be in English and a number, but the opening character cannot be a number. The following definition is wrong:      2myname=VBird    4) variable contents can be combined with double or single quotation marks if there are empty characters. Special characters in    double quotes, such as $, can retain the original characteristics:      "var=lang is $LANG", while the special symbols in lang is en_US    single quotes are general characters:      'var=lang is $LANG', then lang is $LANG    5) the escape character "\" can be used to change the special symbol into a general character, for example,\ $   6) in a series of commands, you also need to provide information through other commands. You can use anti-single quotation marks or $:      version=$ (uname-r)      echo $version    7) if the variable is to increase the content of the variable, it can be added with "$variable name" or ${variable}:      PATH= "$PATH": / home/bin    8) if the variable needs to be executed in other child processes You need to use export to make the variable an environment variable:      export PATH    9) uppercase characters are usually the default variables for the system, and lowercase characters are used if you set your own variables.    10) method to cancel variables: unset myname

Variable access echo[ root @ localhost ~] # echo $PATH/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin [root@localhost ~] # envLC_PAPER=zh_CN.utf8XDG_VTNR=1SSH_AGENT_PID=2807XDG_SESSION_ID=1HOSTNAME=localhost.localdomainLC_MONETARY=zh_CN.utf8IMSETTINGS_INTEGRATE_DESKTOP=yesGPG_AGENT_INFO=/run/user/0/keyring/gpg:0:1TERM=xterm-256colorSHELL=/bin/bashXDG_MENU_PREFIX=gnome-VTE_VERSION=3804HISTSIZE=1000WINDOWID=27262983LC_NUMERIC=zh_ CN.utf8IMSETTINGS_MODULE=IBusUSER=root. HOME: represents the home folder of the user. SHELL: tell us, which SHELL program is currently used in this environment? Linux uses / bin/bash by default! HISTSIZE: this is related to the "history command", that is, the instructions we have given can be recorded by the system, and the number of pens recorded is set by this value. MAIL: when we use the mail command to receive mail, the system will read the mailbox file (mailbox). PATH: is the path to the executable file search. Directories and directories are separated by a colon (:). Since the search for files is sequentially queried by the directories in the variables of PATH, the order of directories is also important. LANG: this is important! Is the language data ~ a lot of information will use him RANDOM: this thing is the "random number" variable! At present, most distributions will have a random number generator, which is the file / dev/random. Use set to observe all variables (including environment variables and custom variables)

The difference between environment variables and custom variables: the env environment can be inherited / used by child processes, and custom variables cannot be inherited / used by child processes.

For example:

Edit a variable in the parent process, and the child process will show a blank.

So use the export command to change it into an environment variable.

Environment variable = global variable custom variable = regional variable

Export: convert a custom variable to an environment variable

For example:

[root@localhost ~] # name=$nameyes [root@localhost ~] # name= "$name" yes [root@localhost ~] # name=$ {name} yes [root@localhost ~] # name=asd [root@localhost ~] # bash\\ enter subroutine [root@localhost ~] # echo $name [root@localhost ~] # exit\\ exit subroutine exit [root@localhost ~] # export name [root@localhost ~] # bash [root@localhost ~] # echo $nameasd [root@localhost ~] # exitexitecho $? To check whether the last command is correct (0 is correct, not 0 is error) [root@localhost ~] # systemctl stop firewalld [root@localhost ~] # echo $? 0 [root@localhost ~] # asdbash: asd: command not found. [root@localhost ~] # echo $? valid range of variable:

The variable after export, we can call it "environment variable"! Environment variables can be referenced by subroutines, but other custom variables will not exist in subroutines. II. Deletion and replacement of functional variable contents of shell variables

First, let the custom setting of lowercase path be the same as the PATH content [root@localhost ~] # path=$ {PATH} [root@localhost ~] # echo ${path} / usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

Suppose I don't like local/bin, so delete the first directory

[root@localhost ~] # echo ${path#/*local/bin:} / usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

Replace uppercase SBIN with variable content sbin of path

[root@localhost ~] # echo ${path/sbin/SBIN} / usr/local/bin:/usr/local/SBIN:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

#: the "shortest" one that matches the replacement of the text

# #: match the "longest" one that replaces the text

Variable testing and content replacement

1. Test whether the variable username exists. If not, give the username content root.

[root@localhost ~] # echo ${username} [root@localhost ~] # username=$ {username-root} [root@localhost ~] # echo ${username} root

two。 If username is not set or is an empty string, set the username content to root

[root@localhost ~] # username= "" [root@localhost ~] # username=$ {username-root} [root@localhost ~] # echo ${username} [root@localhost ~] # username=$ {username:-root} [root@localhost ~] # echo ${username} root

Cancel variable

[root@localhost ~] # name=$aaa [root@localhost ~] # unset $aaa

The way to cancel a variable is to use unset: "unset variable name" such as unsetting myname

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