Linux_shell_3_shell variable features

Source: Internet
Author: User

In the previous two blog posts, we briefly introduced some features of Shell and shell variables. Here we continue our Linux Shell journey.

1. Variable Time and Space Scope

We know that the variables in the C language have time and space scopes. in Linux Shell, the two concepts are extended. Generally, variables have the space and time scope of the current session.

I will simply talk about some of my own understandings, but they may not be completely correct. You are welcome to give me different comments to help me improve my understanding (although I have hung up a volcanol user name, I still feel like Linux is too busy.

Deep ).

A: Generally, shell variables have the time and space scopes of the Current Shell session.

B: The variables through export have the time and space scopes of the current user's login session.

C: Modify/etc/bashrc,/home/username /. bashrc,/home/username /. the bash_profile file is then changed to an environment variable using export with permanent Time and Space scopes.

The system configuration file or personal user configuration file is modified. The time and space scope of the system and individual user are inconsistent.

 

2. Read user input information

Sometimes, when executing a shell script, you need to determine the next step based on the user input information. This requires shell to support reading user input features. In bash, the READ function or

READ command to read user input.

[Volcanol @ volcanol ~] $ Read testthis is a test [volcanol @ volcanol~] $Echo$ Testthis is a test

We can find that the user interaction information is successfully read. The common formats of read commands are as follows:

Read-P prompt information-t wait time (s) variable name

Exp:

[Volcanol @ volcanol ~] $ Read-P"Please input you name:"-T10Nameplease input you name: [volcanol @ volcanol~] $ Read-P"Please input you name:"-T10Nameplease input you name: volcanol [volcanol @ volcanol~] $Echo$ Namevolcanol

When the first line of command is executed, the first half of the second line is output "Please input you name:" But since I have not entered the information, the shell prompt status is displayed after 10 s; then I continue

Run the command on the first line and enter volcanol;

The fourth line shows that my input information is volcanol successfully read from the shell console.

 

3. Variables With types

We know that in C language, variables have types, and in shell, variables can also have types. Use the declare/typeset command in shell to define the variable type.

The syntax format is:

Declare-aixr variable name.

A: define a variable as an array.

I: Define a variable as an integer.

X: equivalent to export.

R: readonly

Exp:

[Volcanol @ volcanol ~] $ Declare-I Sum = 10 + 20 + 30  [Volcanol @ volcanol ~] $ Echo $ Sum  60 [Volcanol @ volcanol ~] $ Sum = 1 + 2 + 3  [Volcanol @ volcanol ~] $ Echo $ Sum  6  [Volcanol @ volcanol ~] $ Unset Sum  [Volcanol @ volcanol ~] $ Sum = 1 +2 + 3  [Volcanol @ volcanol ~] $ Echo $ Sum  1 + 2 + 3 

Through the above example, we can clearly see the role of the-I parameter and the role of the declare command.

 

4. array declaration

As mentioned above, shell supports data types, and shell also supports data structures, typically array. Bash defines an array in the following way:

VaR [Index] = content

Exp:

Test [ 1 ] = "  Test1 "  [Volcanol @ volcanol ~] $ Test [ 1 ] = "  Test2  "  [Volcanol @ volcanol ~] $ Test [ 1 ] = "  Test  "  [Volcanol @ volcanol ~] $ Test [ 1 ] = "  Test1 "  [Volcanol @ volcanol ~] $ Test [ 2 ] = "  Test2  "  [Volcanol @ volcanol ~] $ Test [ 3 ] = "  Test3  "  [Volcanol @ volcanol ~] $ Echo $ Test [ 1 ] $ Test [ 2 ] $ Test [ 3  ] [  1 ] [ 2 ] [ 3  ] [Volcanol @ volcanol ~] $ Echo   "  $ Test [1] $ test [2] $ test [3]  "  [  1 ] [ 2 ] [ 3  ] [Volcanol @ volcanol ~] $Echo   "  $ {Test [1]}, $ {test [2]}. $ {test [3]}  "  Test1, test2. test3 [volcanol @ volcanol ~] $ Echo   "  $ {Test [1]}, $ {test [2]}. $ {test [3]}  "  Test1, Test2. test3 [volcanol @ volcanol ~] $ Echo   "  $ {Test [1]}, $ {test [2]}. $ {test [3]}  " Test1, Test2. test3 [volcanol @ volcanol ~ ] $ [Volcanol @ volcanol ~] $ Echo   "  $ {Test [1]}, $ {test [2]}, $ {test [3]}  "  Test1, Test2, test3 

It can be found that the array reference is different from the general variables. The use is to note the difference, otherwise it is prone to errors.

5. variable extension

In shell, you can also extend the usage of variables, such as extracting the content of variables, intercepting the content of variables, checking whether the variables are set, and then using the expressions to evaluate the value of variables.

5. 1 use the # symbol to intercept the variable content

Exp: intercept variable content

[Volcanol @ volcanol ~] $ Name =/home/volcanol/. Bashrc [volcanol @ volcanol~] $Echo$ {Name ##/*/}. Bashrc [volcanol @ volcanol ~] $ Echo $ {name ##//} home/volcanol/. bashrc [volcanol @ volcanol ~] $ Echo $ {name #/*/} Volcanol/. Bashrc [volcanol @ volcanol~] $ Name = root/home/volcanol/. Bashrc [volcanol @ volcanol~] $Echo$ {Name ##/*//} Root/home/volcanol/. bashrc

If the beginning of a variable is/and there are two/(or more/) in the content of the variable, you can delete all the content between the two/through,

Tip: this does not affect the content of the original variable.

[Volcanol @ volcanol ~] $ Name =/home/volcanol/. Bashrc [volcanol @ volcanol~] $Echo$ {Name ##/*/}. Bashrc [volcanol @ volcanol ~] $ Echo $ {name}/home/volcanol/. bashrc [volcanol @ volcanol ~] $

If a # is used, it has different meanings. For details, refer to the instance.

Exp:

 
[Volcanol @ volcanol ~] $ Unset name [volcanol @ volcanol~] $ Name =/home/volcanol/. Bashrc [volcanol @ volcanol~] $Echo$ {Name #/*/} Volcanol/. bashrc [volcanol @ volcanol ~] $ Name =/home/volcanol [volcanol @ volcanol ~] $ Echo $ {name #/*/} Volcanol [volcanol @ volcanol~] $

Use a # symbol to delete the content between the first pair.

 

5. 2 Use % to intercept variable content

Exp:

 
[Volcanol @ volcanol ~] $ Name =/home/volcanol/. Bashrc [volcanol @ volcanol~] $Echo$ {Name %/*//}/Home/volcanol/. bashrc [volcanol @ volcanol ~] $ Name =/home/volcanol/. bashrc/[volcanol @ volcanol ~] $ Echo $ {name %/*/} [Volcanol @ volcanol~] $

We can find that % is calculated from the back, which is the opposite.

Exp:

 
[Volcanol @ volcanol ~] $ Name =/home/volcanol/. bashrc/[Volcanol @ volcanol~] $Echo$ {Name %/*//}/Home/volcanol [volcanol @ volcanol ~] $

5. 3 similarities

# It is calculated from the past and back, while % is calculated from the back.

#, % Are the longest match

# And % are both short matches.

 

6. $ {var OPR expr} tip: there is no space between VaR and OPR and expr.

This figure shows the resources of laruence. Here, I would like to express my gratitude. I feel that the analysis of so many books is better than that of laruence's Linux tutorial ........ I bought a bird's book and spent 65 oceans.

, The only pity is that there is no CD ).

Exp:

 
[Volcanol @ volcanol ~] $ Unset var [volcanol @ volcanol~] $Echo$ {Var-"Volcanol"} Volcanol

If VaR is not set and expr content is set, $ {var-expr} returns the expr content.

Exp:

[Volcanol @ volcanol ~] $ Var =""[Volcanol @ volcanol~] $Echo$ {Var-"Volcanol"}

If VaR is empty, $ {var-expr} returns the content of Var. Therefore, for $ {var-expr}, if VaR is not set, the value of expr is returned, otherwise, return the value of var.

Exp:

[Volcanol @ volcanol ~ ] $ Unset var [volcanol @ volcanol ~] $ Var = "  Volcanol  "  [Volcanol @ volcanol ~] $ Echo $ {Var :- " Set  "  } Volcanol [volcanol @ volcanol ~ ] $ Unset var [volcanol @ volcanol ~] $ Echo $ {Var :- "  Set  "  } Set [volcanol @ volcanol ~] $ Var = ""  [Volcanol @ volcanol ~] $ Echo $ {Var :- "  Set "  } Set [volcanol @ volcanol ~] $

We can see that the output is added by adding ":" When the variable VAR is empty. In this example, if a non-empty VaR is set, $ {var:-expr} returns the value of VaR; otherwise, expr is returned.

 

For other content, see the table experiment. Here we will not demonstrate it one by one.

 

To be continued .................

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.