Home > Article > Backend Development > Detailed introduction to basic learning of Linux shell scripts (1)
Linux shell script basics learning Here we will start with the first lecture, introducing the basics of shell syntax, beginning, comments, variables and environment variables, to give you a basic introduction, although it does not involve specific things, but laying a good foundation is Learn the premise easily in the future.
1. Basics of Linux scripting
◆1.1 Basic introduction to syntax
1.1.1 Beginning
The program must start with the following line (must be the first line of the file):
#!/bin/sh
The symbol #! is used to tell the system that the parameters behind it are the programs used to execute the file. In this example we use /bin/sh to execute the program.
When you edit a script, if you want to execute the script, you must also make it executable.
To make the script executable:
Compile chmod +x filename so that you can use ./filename to run
1.1.2 Comments
When doing shell programming, sentences starting with # represent comments until The end of this line. We sincerely recommend using comments in your programs.
If you use comments, you can understand the function and working principle of the script in a short time even if you have not used the script for a long time.
1.1.3 Variables
In other programming languages you must use variables. In shell programming, all variables are composed of strings, and you do not need to declare variables. To assign a value to a variable, you can write:
#!/bin/sh
#Assign a value to the variable:
a="hello world"
# Now print the contents of variable a:
echo "A is:"
echo $a
Sometimes variable names are easily confused with other words, such as:
num=2
echo "this is the $numnd"
This does not "this is the 2nd" will not be printed, but only "this is the ", because the shell will search for the value of the variable numnd, but this variable has no value. You can use curly braces to tell the shell that we want to print the num variable:
num=2
echo "this is the ${num}nd"
This will print: this is the 2nd
1.1 .4 Environment variables
Variables processed by the export keyword are called environment variables. We will not discuss environment variables because typically they are only used in login scripts.
That’s it for this lecture. Next we will get into the essential parts of the specific Linux shell script basics.
The above is a detailed introduction to the basic learning of Linux shell scripts (1). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!