Shell getting started tutorial (2)

Source: Internet
Author: User
I. Variables and attributes 1. Define variables and assign Xabc to define variable X and assign abcY to define variable Y. It is null. 2. variable reference. Add the symbol $ before the variable name, for example, the following command shows the echo $ X3 of variable X. The variable attribute variables can have one or more attributes, specifying their internal representation, access or range, or displaying

I. Variables and attributes 1. Define variables and assign X = abc to define variable X, and assign abc Y = to define variable Y. It is null. 2. variable reference, add the symbol $ before the variable name. For example, the following command shows the echo $ X 3 of variable X. The variable attribute variable can have one or more attributes, specifying their internal representation, access or range, or display

I. Variables and attributes

1. Define variables and assign values

X = abc defines the variable X and assigns the value abc.

Y = defines the variable Y, and its value is null.

2. variable reference: add the symbol $ before the variable name. For example, the following command displays the value of variable X.

Echo $ X

3. variable attributes

Variables can have one or more attributes, specifying their internal representation, access, range, or display mode.

Use the typeset command to define variable values and/or attributes

Typeset-attribute variable = value

3.1 lower case (-l) upper case (-u) Attribute

Typeset-l a = ABCD

Echo $

Defines a lower-case variable a and assigns a value to the upper-case string ABCD. The final result is that the variable is actually assigned a value of abcd.

3.2 read-only (-r) attributes

Typeset-r B = 3

B = 4

An error is reported because variable B is read-only and cannot be modified.

3.3 INTEGER (-I) attributes

Typeset-I B = 3

Although you do not need to set this attribute when assigning values to variables as integers, the integer attribute has some benefits, which are described in the following section on arithmetic operations.

3.4 remove the variable attribute and use typeset + attribute, for example:

Typeset + I B

Remove the integer attribute of variable B. In this case, B can be assigned a value as a string, such as abc.

3.5 check the variable attributes. The following command displays all integer variables.

Typeset-I

4. More Methods

Assign the value of a variable to another variable: X = $ HOME

Assign the command output result to the variable: XX = $ (command) or XX = 'command'

For example, XX = $ (date); echo $ XX

Assign the file content to the variable: XX = $ (

Clear variable: unset XX

Ii. Special Parameters

Some special parameters are automatically assigned by shell.

$? Exit status of the last command

Used to determine whether a command is successfully executed. Generally, 0 indicates success, and others indicate failure.

$? Contains the exit status of the last command in the MPs queue.

Example (nofile indicates a non-existent file ):

Date; echo $?

Ls nofile; echo $?

$ Process ID of the Current shell

$-Currently available shell options

$! ID of the process of the last background command or collaboration process

Iii. variable reference

The basic reference method has been mentioned earlier. Add the symbol $ before the variable name, for example, $ X.

To clearly define the variable name, we recommend that you add {} before and after the variable name, for example, $ {X}, which is particularly useful when the variable name is connected to other characters. Example:

X = 3; echo $ X3

Will 33 be displayed ?? There is no result. Shell uses X3 as the name of the variable, but X3 does not exist.

Modify: X = 3; echo $ {X} 3

$ {# Variable} calculates the length of the variable value

X = abc; echo $ {# X}

Result 3

$ {Var:-word} when the variable var is defined and not empty, it is extended to the value of the variable var; otherwise, it is extended to the word

When the $ {var-word} variable var is defined, it is extended to the value of the variable var; otherwise, it is extended to the word

Test: x = abc; echo $ {x:-def}

X =

Echo $ {x:-def}

Echo $ {x-def}

$ {Variable: = word} when the variable is defined and not empty, it is extended to the value of the variable. Otherwise, the variable is set to word.

$ {Variable = word} is defined as the variable value. Otherwise, the variable is set to word.

When the $ {variable: + word} variable is defined and not empty, it is extended to word

$ {Variable + word} variable definition, extended to word

$ {Variable :?} When the variable is defined and not empty, it is extended to the value of the variable. Otherwise, the output parameter null or not set and exits.

$ {Variable ?} When the variable is defined, it is extended to the value of the variable. Otherwise, the output parameter null or not set and exits.

$ {Variable :? Word} when the variable is defined and not empty, it is extended to the value of the variable. Otherwise, the word is output and exited.

$ {Variable? When defining a word} variable, it is extended to the value of the variable. Otherwise, the word is output and exited.

$ {Variable # pattern} Delete the part after the minimum matching pattern from the beginning

$ {Variable # pattern} Delete the part after the largest matching pattern from the beginning

$ {Variable % pattern} deletes the part after the minimum matching pattern from the end.

$ {Variable % pattern} deletes parts after the largest matching pattern from the end

Example:

X = a123

Echo $ {x % [0-9] *}

Echo $ {x % [0-9] *}

$ {Variable // pattern1/pattern2} replace all pattern1 in the variable with pattern2

$ {Variable/pattern1/pattern2} Replace the first pattern1 in the variable with pattern2

$ {Variable/# pattern1/pattern2} if the variable is prefixed with pattern1, replace the first pattern1 in the variable with pattern2

$ {Variable/% pattern1/pattern2} if the variable ends with pattern1, replace the last pattern1 in the variable with pattern2

$ {Variable: start} returns the start part of the variable.

$ {Variable: start: length} returns the length of the variable starting with start.

Example:

X = abc123456

Echo $ {X: 1}

Echo $ {X: 1: 3}

Iv. array Variables

Array variable definition form: typeset aa [0] = 1 aa [1] = 2 aa [2] = 3 aa [6] = 7

Alternatively, the simple aa [0] = 1 aa [1] = 2 aa [2] = 3 aa [6] = 7

Note that the subscript of the array element starts from 0.

$ {Aa} is equivalent to $ {aa [0]}, indicating the first element.

$ {Aa [*]} or $ {aa [@]} indicates all array elements

$ {# Aa [*]} or $ {# aa [@]} indicates the number of array elements.

$ {! Aa [*]} or $ {! Aa [@]}, indicating the array subscript list

Array variable attributes

Like common variables, array variables can also specify attributes, and the typeset command is also used.

Associate an array (strings can be used as the subscript of an array)

Use the typeset-A command, for example:

Typeset-A bb

Bb [dm] = 2

Bb [ff] = 3

Echo $ {bb [dm]}

Echo $ {! Bb [@]}

Echo $ {bb [@]}

V. quotation marks

There are three types of quotation marks: single quotation marks, double quotation marks, and reverse quotation marks

When a variable value contains white space or special characters, quotation marks can be used.

Single quotes and double quotes are similar. Reverse quotes are used to replace command output.

Single quotes

Special characters in single quotes will lose their special meaning and only represent special characters, such as: echo '$'

Double quotation marks

Double quotation marks are similar to single quotes, but do not remove the special meanings of these three special characters: $ '\

To display special characters in double quotation marks, you can add backslash (\) before special characters, such as echo "\""

Backticks are used to replace command output. We have mentioned this before. For example:

Aa = 'date'; echo $ aa

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.