Shell syntax-shell defining variables and calling variables

Source: Internet
Author: User

In the first line of the executable, we usually specify what interpreter to use to execute the file, such as the shell script, the beginning of the file is usually #! /bin/sh

Running the shell will encounter three variables

1. Local variables, defined in scripts or commands, are valid only in the current shell instance, and other shell-initiated programs cannot access local variables.

2. Environment variables, so the program, including shell-initiated programs, can be used to ask environment variables, some programs require environment variables to ensure their normal operation, when necessary, shell scripts can also define environment variables.

3.shell variables are special variables that have shell program settings, part of the shell variables are environment variables, and some are local variables, which ensure the normal operation of the shell.

When defining a variable, the variable name must begin with "A-za-z" , with no spaces or punctuation (can be used with "_", underscore), variable name can not use bash keyword, call variable, only need to precede the variable name with ' $ ' can be called, In view of the problem of the interpreter recognizing the boundary, we usually have a variable name outside the parentheses to determine the variable name.

Deleting a variable can use unset to cancel the definition of the variable.

#代表注释

Now we create a demo file and give it permission to enter the following inside:

After you finish editing the demo.sh script

Give the script a execute permission chmod +x demo.sh

Once you give the script a permission to execute it, you can execute it:/demo.sh can do it.

You can see the results of the execution. Variables have been replaced.

At the end of the script I deleted the variable myName, and I output the variable myName because the variable has been deleted, does not exist, so there is a blank line at the end of the demo.sh script execution;

Shell syntax-shell defining variables and calling variables

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.