Shell Learning Notes (1)-Variables

Source: Internet
Author: User

1, the variables in the shell can be customized, the shell uses the variable with the $

Name= "Shero"
echo "Hi ${name}"

[Email protected]:/home/shero/shell#./test2.sh
Hi Shero

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 (_) without spaces in the middle.
    • Punctuation cannot be used.
    • You can't use the keywords in bash (you can see the reserved keywords using the help command).
    • It is a good programming habit to add curly braces to all variables.
    • The dollar symbol ($) is used when using variables.
    • Variable name does not add dollar sign ($) when defining a variable

2. Delete variables

unset variable Name

eg

Name= "Shero"
Firstname= "Huo"
echo "Hi $name $FirstName"
unset name
echo "Hi $name $FirstName"

Execution Result:

[Email protected]:/home/shero/shell#./test2.sh
Hi Shero Huo
Hi Huo

3. Variables can be redefined:

Name= "Shero"
Firstname= "Huo"
echo "Hi $name $FirstName"
#unset Name
#echo "Hi $name $FirstName"
Name= "Lucy"
echo "Hi $name"

Execution Result:

[Email protected]:/home/shero/shell#./test2.sh
Hi Shero Huo
Hi, Lucy.

4. Variables can be set as read-only variables

Name= "Shero"
Firstname= "Huo"
echo "Hi $name $FirstName"
#unset Name
#echo "Hi $name $FirstName"
ReadOnly Name
Name= "Lucy"
Firstname= "Lee"
echo "Hi ${name} ${firstname}"

Execution Result:

[Email protected]:/home/shero/shell#./test2.sh
Hi Shero Huo
./test2.sh:line 7:name:readonly Variable
Hi Shero Lee

Variable type

When you run the shell, there are three different variables:

1) Local Variables

Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.

2) Environment variables

All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.

3) Shell variables

Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

Shell Learning Notes (1)-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.