1.Linux variables in the shell

Source: Internet
Author: User

1. Shell Variable Type

The shell is a dynamic type language (not using explicit data declarations) and weakly typed languages (variable type operations vary according to requirements). The variables in the shell are non-typed (all string types), but depending on the context, Shell programming also allows comparison operations and integer operations.

2. Classification of Shell variables

There are 3 types of variables in the shell: User variables, positional variables (processing Parameter), environment variables.

1. User variables: Variables defined by the user in the shell programming process, divided into global variables and local variables. By default, a user-defined shell variable is a global variable, and if you want to specify a local variable, you need to use the local qualifier.

2. Positional variables: Positional variables are also called system variables or positional parameters, which are arguments passed to the script when the shell script runs, and also represent the function parameters inside the shell script. Their names are numerically named (for historical reasons, the positional parameters of the direct reference can only be from 0~9, that is, $0~$9, beyond which the range must be enclosed in parentheses, such as ${10}. Special System Variables: $# (number of variables), $? (the end value of the last command), $* (all parameters), [email protected] (synonymous with s*). Note: When s* and [email protected] are in "", their meaning is different, "$*" value is a string, "[email protected]" value is n string.

3. Environment variables: Typically, each process has its own "environment", which consists of a set of variables that contain information that a process may need to reference. In this case, the shell is no different from the normal process. Important Environment variables: path,home,ld_libraray_path (Find the path to the library).

3. Definition and reference of shell variables

Syntax for defining variables: Varname=value, if the value of a variable is more than one word, it must be enclosed in quotation marks.

Syntax for referencing variable values: $varname, ${varname}. $varname is actually a shorthand form of ${varname}.

${varname} This form allows us to use more advanced features of shell string manipulation, as follows:

Substitution operators

Variable operators Replace
${varname:-word}

If varname exists and is not NULL, the value of varname is returned, otherwise, word is returned.

Purpose: Returns the default value if the variable is not defined.

${varname:=word}

Returns the value of Var if varname exists and is not NULL, otherwise, it is not set to Word and then returns its value.

Purpose: Sets the default value if the variable is not defined.

${varname:?message}

Returns the value of VarName if varname exists and is not null; otherwise, print a message and exit the current script.

Purpose: Used to catch errors caused by undefined variables.

${varname:+word}

If varname exists and is not NULL, the value of varname is returned; otherwise, NULL is returned.

Purpose: Used to test the existence of variables.

Each ":" In the previous table is optional, and if ":" is omitted, the "existence and non-null" in each definition is changed to "exists", that is, the variable operator only determines whether the variable exists.

Pattern matching Operators

Variable operators

Replace

${varname#pattern}

If pattern matches the beginning of the varname, the shortest part of the match is deleted and the remainder is returned.

${varname# #pattern}

If pattern matches the beginning of the varname, the longest portion of the match is deleted and the remainder is returned.

${varname%pattern}

If pattern matches the end of the varname, the shortest part of the match is deleted and the remainder is returned.

${varname%%pattern}

If pattern matches the end of the varname, the longest portion of the match is deleted and the remainder is returned.

${varname/pattern/string}

${varname//pattern/string}

Replace the longest part of the matching pattern in varname with srting.

In the first format, only the first part of the match is replaced; in the second format, all the matching parts of varname are replaced.

If the pattern starts with #, it must match the beginning of the varname, and if the pattern% starts, it must match the end of the varname.

If the string is empty, the matching part is deleted.

If varname is @ or *, the operation is applied sequentially to each positional parameter and is extended to the result list.  

4. startup files

The shell uses some startup files to help create a running environment. The files in the/etc directory provide a global setting that overrides the global setting if a file with the same name exists under the home directory.

startup files for different shells

Korn Shell C Shell Bourne Shell Bourne-again Shell

/etc/environment

/ect/profile

$HOME/.profile

$HOME/.KSHRC

/etc/environment

/etc/csh.cshrc

/etc/csh.login

$HOME/.CSHRC

$HOME/.login

/etc/environment

/etc/profile

$HOME/.profile

/etc/environment

/etc/profile

/etc/bashrc

$HOME/.bash_profile

$HOME/.BASHRC

  

  

Variables in the 1.Linux shell

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.