Linux Shell study Note 5

Source: Internet
Author: User

Lesson 6 variables

Part 2 shell programming

7.1 define variables

Format: Name = value # Note: there cannot be spaces on both sides of the equal sign!

This is different from other scripting languages: value can be defined as a variable value without quotation marks, but when the variable value is a string and

 

If space exists, the value must be enclosed in quotation marks. Example: name = "this is value"

 

7.1.2 access value

Access Method: $ name. Add the "$" symbol before the variable name.

 

7.1.3 array Variables

The simplest way to create an array variable is to assign values to each element, for example:

Name [Index] = Value

Here, name is the array name, index is the array subscript, and value is the value of this element.

Name [0] = Apple

Name [1] = banana

Name [2] = Orange

Another definition method is:

Name = (value1 value2 value3)

 

7.1.4 access the array value

$ {Name [Index]} accesses the element of the specified underlying object

$ {Name [*]} accesses all elements in the array

 

7.1.5 Read-Only variables

$ Fruit = Kiwi

$ Readonly fruit

$ Echo $ fruit

Output: Kiwi

$ Fruit = "other value"

Error: fruit: this variable is read only

 

7.2 delete a variable

Unset name

The readonly variable cannot be deleted.

 

7.3 common shell Variables

PWD indicates the current working directory set by the CD command

The UID is initialized and expanded to the current user's digital user ID when started by shell.

Path indicates the search path of the command.

Home current user's start Directory: default parameter of the CD built-in command

 

7.6 terms

Scalar: a scalar can only store one value at a time.

Array variable: it is a mechanism that can be used to group scalar values in bash and KSh.

Local variable: the variable that exists in the current shell instance. It cannot be used for other programs started by the shell.

Environment variable: a variable that can be used for programs started by any shell.

Shell variable: A shell variable is a special variable set by shell. Shell requires this variable for proper function implementation.

 

 

 

Lesson 7 replacement

8.1 Replace the variable.

$ {Parameter:-word} When parameter is empty or not set, use word to replace parameter,

 

Parameter value unchanged

$ {Parameter: = word} When parameter is null or not set, parameter is set to word

$ {Parameter :? Message} When parameter is empty or not set, the message is printed as a standard error.

 

Used to check whether the variables are correctly set

$ {Parameter: + word} When parameteter is set, replace it with word instead of parameter,

 

Parameter value unchanged

 

8.3 command and arithmetic replacement

8.3.1 command replacement

Through the command replacement mechanism, shell can execute a given command set and then replace their output at the command position.

Replacement format:

Newcommandname = 'command'

For example:

Data = 'date'

Users = 'who | WC-l'

Up = 'data; uptime'

 

When $ echo $ data is used, the date command is executed to output the current date.

 

8.3.2 arithmetic replacement

In ksh and bash, shell can execute integer arithmetic operations, which avoids running additional processes such as expr or BC in shell scripts.

 

Perform arithmetic operations in sequence

Run.

Related Article

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.