Variables in Linux

Source: Internet
Author: User

A variable: The space occupied by a contiguous amount of memory: the name of the memory space, called the variable name, the data stored in the memory space, is called the variable value

program = instruction + data

Data: file, existence value (when in memory)


The process of storing data in a memory space (variable) is called an assignment operation; the assignment symbol is usually "="


The types of variables: Different types of variables occupy a different memory space, they can do different operations


Three, variable:

Strong variables: declaration must be implemented before use; Variable type must be declared;

Weak variables: No need to declare before use, and no need to specify variable types


Four, the variable assignment method:

Varname=value

Meaning: A value (value) is stored in a specific memory space by an assignment operation (=)


V. Naming conventions for variable names in bash:

1. The variable name can only be an underscore or a letter starting character, the following other characters can be any alphanumeric or underline

2. The letter in the variable name is case-sensitive

3. Named writing format

All caps

All lowercase

Hump type

Underline connecting words

4. Variable name as far as possible to see the name of understanding

5. Variable names cannot be duplicated with known variables or bash built-in variables


Vi. categories of variables in bash:

Depending on the range of action:

Global variables (Environment variables): scoped to the entire shell process, including its child shell

Local variable: The shell process that is open at the time of the current login, excluding its child shell

Local variables: The scope is only the current program segment, generally used for functions


Depending on how the variable is declared:

Bash built-in variables

Normal built-in variable: histsize

Positional parameter variables: $ $

Special variables: &? $# $* [email protected] $$

$? The state return value of the most recently executed command, the exit status code for the command, indicating whether the command executed successfully

The $# represents the number of positional parameters, excluding the number of the remaining positional parameters of the $, usually expressed in decimal

$* expands from $ start to positional parameter, expands to the value of the first character delimiter for the special variable ifs when using double quotation marks to refer to the expanded result

[email protected] expands from $ start to positional parameter, expands to a separate string when using double quotation marks to refer to the expanded result

$$ Expand the process identifier for the current shell


Seven, custom variables:

1. Declaration and assignment of variables

1) declaring global (environment) variables

Export VARNAME

Export Varname=value

declare [-aaffgilrtux] [-P] [Name[=value] ...]

-a declares an indexed array (if supported)

-a declares associative arrays (if supported)

-I declaration shaping variables

-L declares a variable and converts the letters in the variable to lowercase letters

-U declares a variable and converts the letter in the variable to uppercase

-R declaring read-only variables

-X declares the variable and exports it as a global variable Declare-x VARNAME


2) declaring local variables

Varname=value


3) declaring local variables

Local Varname[=value]


2. View environment variables

Set command: View and change the values of shell properties and view variable names and variable values for shell variables

Export command: View variable names, or assign values to variables

ENV command:


3. View the value of a variable

Echo ${varname}


4. Undo the copy of the variable and the variable declaration

Unset VARNAME


There is a shell configuration file for saving variables:

Common configuration file:

/ETC/BASHRC

/etc/profile

/etc/profile.d/*

Private configuration file:

~/.BASHRC

~/.bash_profile


Note: In general, variables are declared when they are used, and it is not recommended to modify the contents of the configuration file;

Declared variables should be revoked using the unset command after use.


Variables in Linux

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.