[Note]shell variable basis

Source: Internet
Author: User
Tags naming convention truncated

Variable name: Use English, Numbers and _ (underscore), the beginning can not be a number, length unlimited, strictly case-sensitive

Variable settings: variable name = variable Value
Note: No spaces on both sides of the equals sign
You can enclose the value of a variable in single or double quotation marks.
All Chinese symbols (including single and double quotes in Chinese) are not supported
Command output can be obtained using the reverse single quotation mark (above the TAB key) ' Command ' or $ (command)
Use "\" to escape the special word such as \ $! whitespace, etc., into ordinary characters
You can add ' shopt-s-o nounset ' to the script to force the variable to be declared before it can be used, avoiding false input

Variable value obtained: $ variable name or ${variable name}
Note: If a variable is followed by another character that conforms to the naming convention, it must be separated by {} to avoid the shell being wrongly judged

Cancellation of the variable: (Note: cannot operate when the variable property is read-only)
unset variable name or function name
UNSET-V variable Name
Name of unset-f function
Empty variable: Delete the contents of the variable so that its value is null (empty), but the variable still exists
Variable name =
Note: The difference between the cancellation and emptying of a variable (whether the variable exists)

The difference between single and double quotes when set:
Double quotes: Replace the variables in them, command execution results, arithmetic operations results
Note: When you want to use the $ character in double quotes, you need to use \ Escape
Single quotation marks: No action is made

Variable type and scope:
Environment variables: Also called global variables, usually denoted in uppercase characters, all shell environments are valid
Custom variables: Also known as local variables, only valid in the shell where the variable is set, and cannot affect other shell environments
The child process inherits only the environment variables of the parent process
Use the ENV command to view system environment variables
Use the SET command to view all variables (environment variables and user-defined variables)
Use the export variable name to change the user-defined variable to an environment variable

Settings for read-only variables:
ReadOnly or readonly-p: A list of variables showing the current read-only property
ReadOnly variable name: Set read-only variable
readonly-f function Name: Set function is not modifiable
READONLY-A array variable: Set array variable read-only
DECLARE-R variable Name: Set read-only variable
Adjustment of variable properties:
Using the declare command, use your own man query

Deletion and substitution of variable contents: (The picture is cut from the "Bird's-brother private Cuisine Basic Article" Third edition p315)

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/78/A6/wKiom1aAxe-xLf14AADLRHqUYRk115.jpg "title=" Bianliangshanchu. JPG "alt=" Wkiom1aaxe-xlf14aadlrhquyrk115.jpg "/>


Test and replace the contents of variables: (the picture is from "The bird's private private cuisine basic article" The third edition p316)

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/78/A6/wKiom1aAxgigRk9sAAIiwQ_Aim8504.jpg "title=" Bianliangceshi. JPG "alt=" Wkiom1aaxgigrk9saaiiwq_aim8504.jpg "/>


string-related processing:
${variable: Position start}: The string is truncated by the character at the specified position
${variable: Position start: Length}; A string of the specified length is truncated by the character at the specified position
${@: Starting point}: From the specified starting point, obtain all positional parameters of the rear {including start} [position parameter description see Supplemental section]
${@: Start: Number}: Gets the specified number of positional parameters from the specified starting point
${#变量名}: Gets the length of the variable string

Command substitution: Storing the standard output of a command after execution in a variable
1. Variable name =$ (COMMAND)
2. Variable name = ' COMMAND '
Note: The command in Method 2 is not a single quote but an inverted quotation mark (above the TAB key)
For easy viewing, replace with Method 1 is generally recommended.


Add:
$? : The previous command executes the return value, 0 for success, not 0 for execution error

The reference value of the $?

0 Successful exit

>0 exit failed

1-125 Command exit failed, the related value returned by the failure is defined by the program (as in the program exit execution Exit 2 returns 2)

The 126 command was found, but could not be executed

127 Command not found

>128 Command died from a signal.

$$: Current bash shell process number
$! : The process number of the previous daemon
Take "test.sh a B C" as an example
$#: The number of positional parameters, the previous example of the CPC ABC three parameters, that is, $ #值为3
$*: Represents all positional parameters and is treated as a string, the $* value in the previous example is the string "A B C"
[email protected]: represents all unknown parameters, but represents a serial of parameters, the above example [email protected] value "A" "B" "C" three strings
$1-$n: Pass in the program or function parameter, when n>10 need to use ${n}, the above example test.sh is $, a is $, and so on



^_^

This article is from the "junight" blog, make sure to keep this source http://junight.blog.51cto.com/10828785/1729095

[Note]shell variable basis

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.