Shell Tutorial
Shell is a program written in C; it is the bridge between the user and Linux. Shell is both a command language and a programming language.
Shell refers to an application that provides an interface through which users access the services of the operating system kernel.
Ken Thompson's sh was the first Unix Shell, and Windows Explorer is a typical graphical Shell.
Shell Scripts
A Shell script (shell script) is a script program written for a shell.
When people in the industry talk about shell, they usually mean shell scripts, but readers should know that shell and shell script are two different concepts.
For the sake of convention and brevity, "shell programming" appearing in this article refers to shell script programming, not to developing the shell itself.
Shell Environment
Shell programming is like JavaScript or PHP programming: all you need is a text editor to write code and a script interpreter to interpret and execute it.
There are many kinds of Shell on Linux; common ones include:
- Bourne Shell (/usr/bin/sh or /bin/sh)
- Bourne Again Shell (/bin/bash)
- C Shell (/usr/bin/csh)
- K Shell (/usr/bin/ksh)
- Shell for Root (/sbin/sh)
- ......
This tutorial focuses on Bash, also known as Bourne Again Shell. Because it is easy to use and free, Bash is widely used in daily work. At the same time, Bash is the default Shell on most Linux systems.
In general, people do not distinguish between Bourne Shell and Bourne Again Shell, so, for example, #!/bin/sh, it can also be changed to #!/bin/bash.
#! tells the system that the program at the path following it is the Shell program that interprets this script file.
The First Shell Script
Open a text editor (you can use the vi/vim command to create a file) and create a new file test.sh with the extension sh (sh stands for shell). The extension does not affect script execution; just use a meaningful name. If you write a shell script in php, use php as the extension.
Enter some code; the first line is generally like this:
Example
#!/bin/bash
echo
"Hello World !"
Run Example »
#! is a convention marker that tells the system which interpreter this script needs, that is, which Shell to use.
The echo command is used to output text to the window.
There are two ways to run a Shell script:
1. As an executable program
Save the above code as test.sh and cd to the corresponding directory:
chmod +x ./test.sh #使脚本具有执行权限
./test.sh #执行脚本
Note that you must write ./test.sh, not test.sh, and the same goes for running other binary programs. If you write just test.sh, the Linux system will search PATH for a file named test.sh; only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in PATH, and your current directory is usually not in PATH, so writing test.sh will not find the command. Use ./test.sh to tell the system to look in the current directory.
2. As an interpreter argument
In this approach, you run the interpreter directly with the shell script's filename as its argument, for example:
/bin/sh test.sh
/bin/php test.php
Scripts run this way do not need interpreter information on the first line; even if written, it has no effect.