In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand Shell variables in Shell programming". In daily operation, I believe many people have doubts about how to understand Shell variables in Shell programming. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to understand Shell variables in Shell programming". Next, please follow the editor to study!
I. variable operation
a. Variable display, printing
Syntax: echo $var
b. Variable setting
Syntax: var=value
Rules for setting variables:
1. The space character cannot be connected directly at both ends of the variable.
2. Variable names can only be because of letters and numbers, but not numbers at the beginning
3. Special characters in double quotes, such as $, etc., keep the original characteristics.
The code is as follows:
[root@bogon ~] # var= "lang is $LANG"
[root@bogon ~] # echo% var
Root@bogon ~] # echo $var
Lang is zh_CN.UTF-8
1. The special characters in single quotation marks are only general characters.
The code is as follows:
[root@bogon ~] #
[root@bogon ~] # var='lang is $LANG';echo $var
Lang is $LANG
two。 In a series of commands, the information provided by other commands can be provided in single quotation marks' command'or & (command). For example, instruction 1 needs to know the value of instruction 2 in the process of execution, but instruction 1p2 is in a series of instructions.
The code is as follows:
[root@bogon ~] # uname-r
2.6.18-371.el5
[root@bogon] # cd / lib/modules/$ (uname-r) / kernel
[root@bogon kernel] #
3. Accumulation of variables
The code is as follows:
[root@bogon kernel] # var=$ {var} yes
[root@bogon kernel] # echo $var
Lang is $LANGyes
4. Array variable setting and reading
The code is as follows:
[root@bogon ~] # array [1] = a
[root@bogon ~] # array [2] = b
[root@bogon ~] # array [3] = c
[root@bogon ~] # echo ${array [1]}
A
[root@bogon ~] # echo ${array [2]}
[root@bogon ~] # echo ${array [3]}
C
c. Cancel variable (unset)
Syntax: unset var
d. Variable View (set)
Syntax: set
Several more important custom variables
HISTFILE: history storage location
MAILCHECK: how many seconds to scan the mailbox to see if there are any new messages
PS1: prompt settin
$: the current PID of this shell
?: the return code that just finished executing the command. 0 is correct, non-0 is error
For example:
The code is as follows:
Several more important custom variables
HISTFILE: history storage location
MAILCHECK: how many seconds to scan the mailbox to see if there are any new messages
PS1: prompt settin
$: the current PID of this shell
?: the return code that just finished executing the command. 0 is correct, non-0 is error
e. Variable keyboard read (read)
Syntax: read [- pt] var
Options and parameters:
-p: can be followed by a prompt
-t: followed by a description of waiting for input
For example:
The code is as follows:
[plain] view plaincopyprint?
[root@bogon ~] # read atest
This is a test
[root@bogon ~] # echo $atest
This is a test
[root@bogon ~] # read-p "please input.." Attest
Please input.. Hello world = > prompt message
[root@bogon ~] # echo $atest
Hello world
[root@bogon] # read-p "please input.."-t 5 atest
Please input.. = > 5 seconds without input back to command line mode
[root@bogon ~] #
[root@bogon ~] # echo $atest
Hello world
f. Variable declaration (declare)
Syntax: declare [- aixr] var
Options and parameters
Nothing is received after declare, which means that all variables are queried, and the function is the same as set.
-a: define the following variable named variable as an array type
-I: define the following variable named variable as an integer number (integer) type
-x: use the same as export, but change the following variable into an environment variable
+ x: change the environment variable to a custom variable
-r: configure the variable to be of type readonly, which cannot be changed or unset (you need to log out before logging in to change it back)
For example:
The code is as follows:
[plain] view plaincopyprint?
[root@bogon ~] # echo $sum
100, 50, 50, 10 = > is treated as a string by default.
[root@bogon] # declare-I sum=100+50+10
[root@bogon ~] # echo $sum
Declare it as int, so you can add
[root@bogon ~] # declare-x sum
[root@bogon ~] # export | grep sum
Declare-ix sum= "160,160,160,160,160,160,160,160,160,160,160,160,160.
[root@bogon ~] # declare + x sum
[root@bogon ~] # export | grep sum= > query cannot be an environment variable
[root@bogon] # declare-r sum;sum=test
Bash: sum: readonly variable = > read-only allows modification
g. Variable content deletion
Grammar
${var#/key}: delete the one that meets the shortest key from the destination.
${var##/key}: delete the one that matches the longest key from the destination
${var%/key}: delete the one that meets the shortest key from back to front
${var%%/key}: delete the one that meets the shortest key from back to front
For example: ${var#/key}
The code is as follows:
[plain] view plaincopyprint?
[root@bogon ~] # path=$ {PATH}; echo $path
/ usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@bogon ~] # echo ${path#/*:} = > key is *. (* is a wildcard)
/ usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
For example: ${var##/key}
The code is as follows:
[plain] view plaincopyprint?
[root@bogon ~] # path=$ {PATH}; echo $path
/ usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@bogon ~] # echo ${path##/*:}
/ root/bin
h. Variable content substitution
Syntax:
${var/ old string / new string}: replace the first string that meets the criteria
${var// old string / new string}: replace all strings that meet the criteria
For example: ${var/ old string / new string}
The code is as follows:
[plain] view plaincopyprint?
Root@bogon ~] # path=$ {PATH}; echo $path
/ usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@bogon ~] # echo ${path/sbin/SBIN}
/ usr/kerberos/SBIN:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
For example: ${var// old string / new string}
The code is as follows:
[plain] view plaincopyprint?
[root@bogon ~] # path=$ {PATH}; echo $path
/ usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@bogon ~] # echo ${path//sbin/SBIN}
/ usr/kerberos/SBIN:/usr/kerberos/bin:/usr/local/SBIN:/usr/local/bin:/SBIN:/bin:/usr/SBIN:/usr/bin:/usr/X11R6/bin:/root/bin
Second, environmental variables
Ordinary variables can be understood as local variables, environment variables can be understood as global variables, and the bash shell obtained successfully by login is a process. In this case, opening a new SHELL is its child process. The child process cannot obtain the custom variables of the parent process, but it can obtain the environment variables of the parent process.
a. Environment variable export (export)
Syntax: export var
b. Environment variable View (env)
Syntax: env
Several important environmental variables
HOME: the home folder that represents the user
SHELL: represents which program shell is currently in use, and I am using / bin/bash now
HISTSIZE: maximum number of storage entries in history
The mailbox file that the system will read when MAIL:mail commands the system to receive the mail.
PATH: perform file lookup path
LANG: language family information
RANDOM: random number variable (0,32767)
Third, the setting of prompt (PS1)
The variable PS1=' [\ u @\ h\ W]\ $'records the display format of the command prompt [root@bogon ~] #
Symbolic meaning
\ d: the date format of [week, month, day] can be displayed, such as "Mon Feb 2"
\ H: full hostname.
\ h: only take the hostname before the first decimal point
\ t: display time, in 24-hour format [HH:MM:SS]
\ T: display time, in 12-hour format [HH:MM:SS]
\ a: display time, in 24-hour format [HH:MM]
\ @: display time, [am/pm] style in 12-hour format
\ U: account name of the current user, such as [root]
\ v: BASH version information, such as Bird's test motherboard is 3.2.25 (1), only take [3.2] to display
\ w: the full working directory name, the directory name written from the root directory. But the home catalogue will be replaced by ~
\ W: use the basename function to get the working directory name, so only the last directory name is listed.
\ #: the number of orders issued.
\ $: prompt character, if it is root, prompt character is #, otherwise it is $
For example:
The code is as follows:
[root@bogon ~] # PS1=' [\ u@\ h\ A\ W\ #]\ $'
[root@bogon23:45 ~ 82] #
At this point, the study on "how to understand Shell variables in Shell programming" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.