Shell scripts define references to variables and environment variables

Source: Internet
Author: User

First, define variables
When defining a variable, the variable name does not have a dollar sign ($), such as:

Variablename= "Value"


Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with. At the same time, the name of the variable names must follow the following rules:

the first character must be a letter (a-z,a-z).
You can use an underscore (_) in the middle without spaces.
? punctuation cannot be used.
? You cannot use the keywords in bash (you can view the reserved keywords using the help command).
The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:

For skill in Ada coffe Action Java
Do
echo "I am good at ${skill}script"
Done
If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null) and the result of the code execution is not what we expect it to look like.
It is a good programming habit to add curly braces to all variables.
Redefining variables
A defined variable can be redefined, such as:

Your_name= "Tom"
Echo $your _name
Your_name= "Alibaba"

Echo $your _name


This is legal, but note that the second assignment can not be written $your_name= "Alibaba", the use of variables when the dollar symbol ($).


II. referencing environment variables and variables


1. The following is a reference to a variable method in a file

#!/bin/sh

Log= "4"
if [$log = "4"];
Then
echo "$log is succed"
Else

echo "$log is failure"
Fi


2. Referencing variables from other scripting programs

[email protected] tmp]# cat 1.sh
log= ' basename '. Log
Echo $log in

[email protected] tmp]# cat 2.sh
Source 1.sh
Echo $log in

[email protected] tmp]#./1.sh
1.sh.log in./1.sh

[email protected] tmp]#./2.sh
2.sh.log in./2.sh
2.sh.log in./2.sh


3. Direct reference to environment variables, so as to facilitate us to modify and set

I put the environment variables in the/etc/profile file of the Development Board.

Export log=4

Directly referenced in other scripts, preceded by the environment variable path source/etc/profile

You can enter export log=4 directly at the command line for modification

Shell scripts define references to variables and environment variables

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.