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/02 Report--
Editor to share with you how to use the declare command in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Linux commonly used command declare command is used to declare shell variables, declare is the shell instruction, in the first syntax can be used to declare variables and set the properties of variables ([rix] is the attribute of variables), in the second syntax can be used to display the shell function.
Declare declares variables, sets or displays the values and properties of variables.
Syntax declare [- aAfFgilnrtux] [- p] [name [= value]...] The main purpose is to display all variables and values containing the specified attribute
Displays one or more variables and values containing the specified attribute
Display the properties and values of one or more variables
Displays the properties and values of all variables and displays the definition of the function
Display the properties and values of all variables
Display the properties and values of all global variables
Show all function names and function definitions
Show only all function names
Display one or more function names and function definitions
Only one or more function names are displayed
Declare global variables (optional: assignment)
Declare variables (optional: assignments, attributes)
Add or delete attributes of variables (optional: assignment)
The option-f restricts the operation or display to function names and function definitions. -F displays only the function name (append line number and source file when debugging). Global variables are created when-g is used in the shell function; otherwise ignored. -p displays the properties and values for each name. * options for setting properties:-a create an array (if supported). -A creates an associative array, if supported. -I add integer attributes. + I delete the integer attribute. -l adds a lowercase attribute, and the value of the variable is converted to lowercase. + l removes the lowercase attribute. -n add reference attribute (if this option exists). + n Delete reference attribute (if this option exists). -r adds a read-only attribute. -t adds tracking attributes. + t deletes the tracking attribute. -u adds an uppercase attribute, and the value of the variable is converted to uppercase. + u removes the uppercase attribute. -x added export attributes. + x Delete the export attribute. Parameter name (optional): variable name or function name. Value (optional): the value of the variable. The return value declare returns true unless you provide an illegal option or an incorrect assignment. For the specific circumstances that cause the exception, please see the discussion section on the exception.
Example # declares variables, and of course you are welcome to query the linux command on this site (thanks to the project sponsor @ jaywcjlove). Declare reference_website=' https://wangchujiang.com/linux-command/' # displays all variables and values that contain integer attributes. Declare-I # defines the variable b and assigns it to 3, with an integer attribute. Declare-I bounded 5 # displays properties and returns declare-I b = "5". Declare-p b # removes the integer attribute. Declare + I b # displays properties and returns declare-- b = "5". Declare-p b # forcibly converts the case of the value according to the variable attribute. Declare-u uc_var='abc' declare-l lc_var='ABC' # displays the global variable function test () {declare-g aquire3 # or local-g baked 3 # or cased 3 # within the 'ABC abc'; echo "${uc_var} ${lc_var}" # definition function. Let's look at their properties. Declare-p a b c} # executes the function. Test # returns the result. # declare-- a = "3" # declare-- b = "3" # declare-- c = "3" # the global variable outside the definition function, declare aqui3bici3 declare-p a b #, returns the following result. # declare-- a = "3" # declare-- b = "3" # defines the local variable function test2 () {local-I aqui3 declare-I baked 3} test2 # does not have this variable (which has been destroyed) echo "${a} ${b}" # so the most common analogy in our daily scripts is to actually declare and assign a global variable. In the following * * discussion * * session, we will extend the discussion of global and local variables. # Note: you cannot cancel an array using `+ a` or` + A`, nor can you use `+ r` to cancel the read-only attribute. # define a read-only array and define the assignment while setting the property. Declare-ar season= ('Spring'' Summer' 'Autumn'' Winter') # or this. Season= ('Spring'' Summer' 'Autumn'' Winter') declare-ar season # displays all arrays. Declare-a # defines an associative array. Declare-A fruits= (['apple'] =' red' ['banana'] =' yellow') # displays all associative arrays. Declare-A # shows the properties and values of all variables and the definition of the function. The output is very long. Declare # displays the properties and values of all variables. Declare-p # displays the properties and values of all global variables. Declare-g # displays all function names and function definitions. Declare-f # displays only all function names. Declare-F # defines two functions. Function func_a () {echo $(date + "% F% T");} function func_b () {cd /; ls-lh-- sort=time;} # shows one or more function names and function definitions. Declare-f func_a func_b # displays only one or more function names, which is useful when verifying that a name is already defined as a function. Declare-F func_a func_b # is best not to let the function name be the same as the variable name. Discuss global and local variables
As the above example points out, we need to understand these concepts when writing programs every day. Here is a brief introduction, of course, you can also easily search for relevant content.
It is recommended to use the local command within the function and the declare command outside the function.
Do not define too many global variables in the script, which may have unintended consequences by other function calls and are not easy to check out.
Not to mention the lack of necessary comments-- ZhuangZhu-74
Related information:
Coding specification provided by google
Discussion on global variables
Global variable: exists throughout the execution of the script as long as it is not deleted.
Local variable: defined within a function and deleted after the function is executed.
About the declare typeset export local readonly command
Why do you need to define other commands for what declare can do?
Because this makes the meaning of the statement clearer, for example:
Typeset is the same as the declare command.
When you set variables for exported properties, export var and declare-x var.
Use local when declaring variables within a function.
Declare read-only variables, using readonly.
About the abnormal situation
Declare fails for a variety of reasons, for which you can refer to the declare section of the bash online documentation (the latest version), or execute info bash to check the last long list of sentences at the beginning of the an attempt is in the declare section.
The above is all the contents of the article "how to use declare commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.