Shell script learning notes (2)-Variables

Source: Internet
Author: User

Variables and strings

Bash shell does not have the so-called "Data Form" unless the built-in declare is used to specify the variable type. In Bash shell, there is only one data type: A string consisting of Characters

Use it without having to go through the stated procedure

After the variable value is set, the variable value is valid only in the current shell environment. Variable name length is unlimited, case sensitive.

Variable setting method: variable name = value, but the good habit is to enclose the string with double quotation marks or single quotation marks. No white space is allowed on both sides of the equal sign, and the white space on the right should be enclosed in quotation marks.

To avoid errors, you can specify that all variables must be used by a set program. Method: shopt-s-o nounset

Get the variable value: $ variable name or $ {variable name}. If the variable is followed by another English, number word or bottom line, use {} to separate $ from the variable name, for example, $ {myname} Mmm. Otherwise, you do not need

Echo printf: DISPLAY variable values

Cancel variable:
Unset variable name: Find the variable first and then find the Function
Unset-v variable name cancel variable
Unset-f function name cancel Function

Empty variable: variable name = its value is null (null), but the variable still exists

Variables enclosed in double quotation marks can be used: replace variables; replace command execution results; replace arithmetic operation results
A single quotation mark is used to form a WYSIWYG string (which is displayed when you write something) without any operations to replace variables.

The execution scope of the variable, which is only valid in the corresponding shell environment.

When the variable becomes an environment variable, you can use the export declare-x variable = "" for the sub-shell ""

Cancel environment variable: testVar = or unset testVar

Various Bash built-in Variables

Read-Only variable: readonly variable name readonly-f function name readonly-a array name declare-r variable name declare method for adjusting variable attributes

Alias: alias = the instruction cannot be blank. The right side is sometimes enclosed in single quotes and can be recursively defined.
Cancel alias: unalias alias

Array: The subscript starts from 0 and displays or obtains the value of an element: $ {array name [index value]}
You can also specify indexes for individual elements, such as: c = {[3] = 10, [1] = 20, [10] = 30}
Retrieve all elements of the array: $ {B [@]}, an empty string or $ {B [*]}
Get the number of array elements: $ {# array [@]} or $ {# array [*]}
If the array element is a string, $ {# array [Index]} obtains the length of an element.
Unset A [I]

Use Here Document to package original codes such as C. A prototype carrying an attack program.

Advanced Variables

Variable extension is a simple condition judgment. Different Conditions are assigned with different substitution values for variables. That is, variable extension adds conditional judgment to the basic type, if the conditions are met (if the conditions do not exist or the values are null), replace the variables, it is actually an implicit program Syntax of "if-then-else ".
Definition of "variable existence": If a variable has been set a value, it is called a variable. If a variable has never been set a value, or you have used unset to cancel a variable, the variable does not exist or is not defined.
Test variable "exist or not": $ {variable to be tested-default value}
The test variable "does not exist" or its value is null:
"Return" a default value $ {variable to be tested:-default value} Main Purpose: this variable must have a value in script, therefore, the result of variable extension must return a non-null value (use the default value or variable value)
[-N # {DEBUG:-}] & set-v
Set a default value for the variable
Error message: $ {variable to be tested :? Prompt information} display the variable name and prompt information, and immediately stop executing the script
Usage of the conditional statement: make sure that a variable value exists. Otherwise, the execution may have serious consequences.
The "existence" :$ {variable to be tested: + true value} of the test variable is used to test whether something is true.
Summary:


Take string slices, Length: $ {Variable: position start point}, $ {Variable: position start point: length}
Take part of the Location Parameter $ {@: start}, $ {@: Start Point: Count}
Length: $ {# variable name}

Comparison style:
Delete the shortest $ {Variable # style} from the previous comparison; Delete the longest $ {Variable # style}
Delete the shortest $ {Variable % style} from the end; Delete the longest $ {Variable % style}
Replace or delete some strings. Only the first matching string $ {Variable/style/replacement string} is replaced. All matching strings $ {Variable // style/replacement string} are replaced }; only the first matching string $ {Variable/style/} is deleted, and all matching strings $ {Variable // style/} are deleted /}
Summary:

Get the variable name list and array index list:
$ {! Array variable [@]} or $ {! Array variable [*]}
$ {! Start string @]} or $ {! Start with string *]}

Command replacement: variable name = $ (command) or variable name = 'COMMAND '.

Arithmetic Extension: $ (arithmetic expression ).

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.