English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Shell is a program written in C language, which is the bridge for users to use Linux. Shell is both a command language and a programming language.
Shell refers to an application that provides an interface for users to access the services of the operating system kernel.
Ken Thompson's sh is the first Unix Shell, Windows Explorer is a typical graphical interface Shell.
Shell script (shell script) is a script program written for Shell.
The Shell commonly referred to in the industry is usually Shell script, but readers should know that Shell and Shell script are two different concepts.
Due to habit, for brevity, the 'shell programming' mentioned in this article refers to shell script programming, not the development of shell itself.
Shell programming is similar to JavaScript and php programming, as long as you have a text editor that can write code and a script interpreter that can execute the script.
There are many types of Shells in 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, which is Bourne Again Shell, due to its ease of use and free of charge, Bash is widely used in daily work. At the same time, Bash is also the default Shell of most Linux systems.
In general, people do not distinguish between Bourne Shell and Bourne Again Shell, so, like #!/bin/sh, it can also be changed to #!/bin/bash.
#! Informs the system that the program specified by the subsequent path is the Shell program that interprets this script file.
Open a text editor (you can use vi)/The vim command is used to create files, create a new file named test.sh with the extension sh (sh represents shell), the extension does not affect the execution of the script, just as the name suggests. If you write a shell script in php, use the extension php.
Enter some code, the first line is usually like this:
#!/bin/bash echo "Hello World !"
#! It is an agreed-upon marker that tells the system what interpreter the script needs to run, that is, which Shell to use.
The echo command is used to output text to the window.
1As an executable program
Save the above code as test.sh and cd to the corresponding directory:
chmod +x ./test.sh #Make the script executable ./test.sh #Execute the script
Note, it must be written ./test.shand not test.shIn the same way, running other binary programs is also direct, just write test.sh, and the Linux system will go to PATH to find if there is a test.sh, and only /bin, /sbin, /usr/bin,/usr/sbin and others are in PATH, your current directory is usually not in PATH, so writing test.sh will not find the command, and you need to use ./test.sh tells the system to look for it in the current directory.
2As an interpreter parameter
This running method is to run the interpreter directly, and its parameters are the file name of the shell script, such as:
/bin/sh test.sh /bin/php test.php
Scripts running in this way do not need to specify interpreter information in the first line, and it is useless even if it is written.