Fun bash Script: Basic Concepts and initialization of Variables

Source: Internet
Author: User

2nd articles

Basic Concepts
Name

Note the following rules:

  • It can only contain letters, numbers, and underscores, and cannot start with a number.
  • Case Sensitive
  • The name cannot be the same as the name of the system variable.

In addition, if you want to export the name as a global variable, it is best to use uppercase letters for the name. This is just a customary convention, not a strict constraint.

Type

Almost all programming languages have the concept of variables, and bash is no exception. When talking about variables, there is no need to talk about types. However, unlike other languages, bash is a non-type language. To say a type, it can be considered as a string. Speaking of this, you may say that Bash also has an integer type, as if it had an integer operation. However, some operators, expressions, or commands are used to treat a string as an integer. In essence, they are strings.

New Variable

Bash variables, like other scripting languages, do not need to be declared,Use it directly. So the first appearance of variables is accompanied by initialization. AboutInitializationThe second part of this article provides a detailed introduction.

Use Variables

Add$Symbol. It is called the dollar symbol. This is the most basic way to use data.

a="hello world"echo $a
The producer prints Hello world. Of course, you can put variables and constant strings together for printing, such

boy=Jellyecho "$boy:hello world!"
The result is JELLY: Hello world!

Initialization/Assignment

The variables in bash are initialized for the first time because they do not need to be declared. There are three methods to change the initialization (or value assignment) in Bash: direct assignment, read input, and command replacement.

Direct assignment

Direct assignment uses an equal sign, which is also common in other programming languages, such:

a=123b=abcc=‘123’d=“hello world”
Note Space

Note thatThere cannot be spaces around the equal sign!!! Programmers with programming experience in other languages may often add spaces to make the Code look beautiful. However, this is not allowed in bash.

The reason for this restriction is that if you write a = 123. Then the system will regard it as if you are executing a command and both = and 123 are parameters of this command. Being able to directly call various system commands within the script is a different feature of bash, and there is no hard rule that equal sign = cannot be used as a command parameter.

Quotation marks

The second thing to understand is that since all the variables mentioned earlier can be considered strings, A = 123 and A = "123" are actually no different. However, if your variableContains SpacesIt must be enclosed in quotation marks.

Single quotation marks and double quotation marks are generally used in the same way, except for some special cases:

a="I'm Jelly"b='Jelly:"Hi"'echo $aecho $b
When a string contains single quotes, double quotation marks are used outside, and vice versa.

Read Input

The input of the read terminal assigns a value to the variable, that is, the READ command is used. Both read and ECHO are embedded commands. View the Code directly:
echo -n "Please Input your name:"read nameecho "Hi,$name,welcome to uncle Jelly's cabin!"

Alternatively, you can use the-P option of the read command to simplify the above Code:
read -p "Please Input your name:" nameecho "$name,welcome to uncle jelly's cabin!"
The direct result is the same as the previous code. For more instructions on the READ command, Run man read on your own.
Command replacement is a very practical method of initialization or assignment. Assign values to a variable using the output of other commands. This requires the anti-reference symbol. ''This is the symbol on the left of number key 1, not a single quotation mark.
Dir = 'pwd' Tim = 'date' echo "I am under the $ dir directory" Echo "now the time is $ time"
The PWD and date commands are called here. You can directly type these two commands on the terminal to view the printed results. This is to assign a value to the result.
Note:
Some commands print a row in a row, but when the value is assigned via the back quotes, the \ n character is not saved in the variable and will be replaced by spaces, in this way, we can find that each row is printed to a row. I am too lazy to post code. Let's look at this process directly:

Others

Other initialization methods, such as using some special variables. For example, random, this is a special variable. Every time you echo $ random. The print structure is different. Of course, you can use it to assign random values to variables.

a=$RANDOMb=$RANDOMc=$RANDOM
Print the values of these three variables.

Do you understand? This tutorial is over. See you next time. If you think I can write more, please try again. This is my greatest support.

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.