The variables in bash

Source: Internet
Author: User

Bash Variable type:
Environment variables
Local variables (local variables)
        Positional variables
        Special variables (built-in)

Local variables:
Varname=value: Scopes are available for the entire bash process;
        Variable Naming conventions:
        1. Can only contain letters, numbers and underscores, and start with letters and underscores
        2. It is best not to duplicate the system's existing environment variables
        3. See the meaning of the name

Local variables:
Local Varname=value: Scope is the current code snippet;

Environment variable: The scope is the current shell process and its child processes and cannot affect its parent process;
Export Varname=value, if the variable is already defined, it can only be exported varname with the variable name, i.e.
        1. Export Varname=value
        2. Varname=value
            Export VarName
        The script launches a child shell environment variable when the command is executed:
        Scripts that are automatically executed by the system (not command-line startup) require a self-defined environment variable;


Positional variables:
$1,$2,$3, ...
                Cat first.sh Test.txt Hello
                $1:first.sh
                $2:test.txt
                $3:hello
        Shift: kicks off parameters
                Shift N: kicks off n parameters and kicks off a parameter by default


Special variables:
$?: The return value of the previous command execution state:

                There are two possible types of return values for program execution:
                        1. Program execution Results
                        2. Does the program state return (0-255)
                                0 to perform the correct
                                1-255 Execution Error (1,2,127 system reservation);

        $#: Gets the total number of parameters in the current shell command line
        $*: Gets all the parameters of the current Shell "$ ..., under IFS control
        [Email protected]: All parameters of this program "$1″" $2″ "$3″" ... ", not controlled by IFS
        $ A Gets the file name of the currently executing shell script
        $n gets the nth parameter value of the currently executing shell script, n=1..9
        $$ gets the current shell's process number (PID)
        $! PID that executes the previous instruction




Bash:

Reference variable: ${varname}, parentheses can be omitted


Undo Variable:
unset varname

To view variables in the current shell:
Set
        Include environment variables and local variables

To view environment variables in the current shell:
        1. printenv
        2. Env
        3. Export

The variables in bash

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.