Chapter II Variables of Shell programming

Source: Internet
Author: User

Shell programming variables a bash variable and variable classification

Bash is a standard shell in Linux, so it's often called bash as a shell.

1. Variable naming rules
    • Variable names can consist of letters, numbers, underscores, but must begin with a letter and an underscore;
    • The length of the variable name cannot exceed 255 characters;
    • Variable names must be unique in the valid range;
    • In bash, the default types of variables are string-type;
2. Variables are categorized by storage type

The default type is a string type.

    • String type, the value of single-double quotation marks can be;
    • Plastic
    • floating point type;
    • Date type;
Two-User custom variables

Variable names and variable values are user-defined, meaning they can be modified by user control.

1. Variable definition

Variable name = variable Value

Note : "=" cannot have spaces around.

2. Variable invocation

ECHO is the simplest variable invocation and can only be used as output.

such as: Echo $ variable Name

3. Variable Overlay

For example:

X=123

x= "$x" 456

x=${x}789

Output 123456789 when echo $x

Useful: Add environment variables, such as path paths.

4. Variable view

Set

Ability to query all variables currently running on the system, including system environment variables and user-defined variables for the current shell.

Option:-U, if you set this option, and then use Echo to invoke an undeclared variable, you will get an error.

If the y variable is not declared assigned, a blank line is printed when you use the Echo $y, and if you first use Set-u, then echo $y will prompt for the variable.

5. Variable deletion

unset

How to use: unset x,unset does not delete the value inside the variable, but instead deletes the variable, so the variable name does not need to be added $.

Three environment variables

The bash command can be used to enter the current Shell's child shell, you can view the current shell through Pstree, this method can be used to do the following environment variables and user-defined variables in the effective area of the experiment.

1. Differences between environment variables and user-defined variables

Environment variables are global variables, and user-defined variables are local variables.
User-defined variables take effect only in the current shell, and the environment variables take effect in the current shell and all child shells of the shell.
The user can customize the environment variables, but the environment variable names and variables that are in effect for the system are fixed.

2. User-defined environment variables

$ export Variable name = variable Value
or $ variable name = variable Value
For example: $ export Variable name

3. View Environment variables

$ set View all variables
$ ENV View Environment variables

4. Delete environment variables

$ unset environment variable

5. PS1 Environment variables

Chapter II Variables of Shell programming

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.