What is a Shell script?

A shell script (English: Shell script) is a computer program and text file whose content consists of a series of shell commands, and it operates after being interpreted by the Unix Shell. It is designed as a scripting language, and its operation is similar to that of an interpreted language. The Unix shell acts as the command-line interpreter, and after reading the shell script, it runs the shell commands in sequence, then outputs the results. Shell scripts can be used for system administration, file operations, and so on.

Shell scripts exist in Unix and all Unix-like systems, such as Linux, FreeBSD, and other operating systems. Depending on the different types of Unix shells, shell scripts also have various dialects. Batch files in DOS, OS/2, and Microsoft Windows have similar functionality to shell scripts.

Let's look at an example

#!/bin/sh
cd ~
mkdir shell_tut
cd shell_tut

for ((i=0; i<10; i++)); do
    touch test_$i.txt
done

Example analysis:

  • Line 1: Specify the script interpreter; here /bin/sh is used as the interpreter.
  • Line 2: Switch to the current user's home directory.
  • Line 3: Create a directory shell_tut.
  • Line 4: Switch to the shell_tut directory.
  • Line 5: Loop condition, loops a total of 10 times.
  • Line 6: Create a test_1...10.txt file.
  • Line 7: End of loop body.

cd, mkdir, and touch are built-in system programs, generally located in /bin or /usr/bin directories. for, do, and done are keywords of the sh scripting language.


Environment

Shell programming is like Java and PHP programming; you just need a text editor to write code and a script interpreter to interpret and execute it.

Current mainstream operating systems all support shell programming. The shell programming described in this document refers to shell under Linux, and basically discusses functions under the POSIX standard, so it also applies to Unix and BSD (such as Mac OS).

Linux

Linux comes with a shell interpreter by default.

Mac OS

Mac OS not only comes with the two most basic interpreters, sh and bash, but also includes less commonly used interpreters such as ksh, csh, and zsh.

Emulators on Windows

Windows does not have a built-in shell interpreter when shipped; you need to install one yourself. In order to also use tools such as grep, awk, and curl, it is best to install Cygwin or MinGW to emulate a Linux environment.


Script interpreter

sh

This is the Bourne shell, the POSIX (Portable Operating System Interface) standard shell interpreter. Its binary file path is usually /bin/sh, and it was developed by Bell Labs.

bash

Bash is a replacement for the Bourne shell, belongs to the GNU Project, and its binary file path is usually /bin/bash. In the industry, bash, sh, and shell are often used interchangeably. For example, you will often see in recruitment posts for operations engineers: familiar with Linux Bash programming, proficient in Shell programming.

In CentOS, /bin/sh is a symbolic link pointing to /bin/bash:

[root@centosraw ~]# ls -l /bin/*sh
-rwxr-xr-x. 1 root root 903272 Feb 22 05:09 /bin/bash
-rwxr-xr-x. 1 root root 106216 Oct 17  2012 /bin/dash
lrwxrwxrwx. 1 root root      4 Mar 22 10:22 /bin/sh -> bash

But this is not the case on Mac OS; /bin/sh and /bin/bash are two different files, although their sizes differ by only about 100 bytes:

iMac:~ wuxiao$ ls -l /bin/*sh
-r-xr-xr-x  1 root  wheel  1371648  6 Nov 16:52 /bin/bash
-rwxr-xr-x  2 root  wheel   772992  6 Nov 16:52 /bin/csh
-r-xr-xr-x  1 root  wheel  2180736  6 Nov 16:52 /bin/ksh
-r-xr-xr-x  1 root  wheel  1371712  6 Nov 16:52 /bin/sh
-rwxr-xr-x  2 root  wheel   772992  6 Nov 16:52 /bin/tcsh
-rwxr-xr-x  1 root  wheel  1103984  6 Nov 16:52 /bin/zsh

High-level programming languages

Theoretically, as long as a language provides an interpreter (not just a compiler), that language can be used for script programming. Common interpreted languages can all be used for script programming, such as Perl, Tcl, Python, PHP, and Ruby. Perl is the most established script programming language, and in recent years Python has also become a preinstalled interpreter in some Linux distributions.

Compiled languages can also be used for script programming as long as they have an interpreter. For example, C shell is built-in (/bin/csh), Java has a third-party interpreter JShell, and Ada has a paid interpreter AdaScript.

The following is an example of a PHP shell script (assuming the file is named test.php):

#!/usr/bin/php
<?php
for ($i=0; $i < 10; $i++) {
    echo $i . "\n";
}

Execute:

/usr/bin/php test.php

Or:

chmod +x test.php
./test.php

How to choose a shell programming language

Familiar vs Unfamiliar

If you already master a programming language (such as PHP, Python, Java, JavaScript), it is recommended that you directly use this language to write script programs. Although some places may be a bit verbose, you can leverage your experience in this language field (unit testing, single-step debugging, IDEs, third-party libraries).

The additional learning cost is very small; you only need to learn how to use the shell interpreter (JShell, AdaScript).

Simple vs Advanced

If you feel that writing shell scripts in a language you are familiar with (such as Java, C) is too verbose, and you just want to do things like backing up files, installing software, or downloading data, then learning to use sh or bash would be a good idea.

Shell only defines a very simple programming language, so if your script has a high level of complexity, or the data structures you need to manipulate are relatively complex, then you should use scripting languages like Python or Perl, or a high-level language you are already good at. Because sh and bash are very weak in this regard, for example:

  • Its functions can only return strings, not arrays.
  • It does not support object orientation, so you cannot implement some elegant design patterns.
  • It is interpreted, executing while interpreting, and does not even have precompilation like PHP. If your script contains errors (for example, calling a function that does not exist), as long as execution has not reached that line, no error will be reported.

Environment compatibility

If your script is provided for other users to use, using sh or bash will give your script the best environment compatibility. Perl has long been a standard component of Linux, and Python has also become a standard component of some Linux distributions in recent years. As for Mac OS, it comes with mainstream programming languages such as Perl, Python, Ruby, PHP, and Java installed by default.


Your first shell script

Writing

Open a text editor, create a new file with the extension .sh (sh stands for shell). The extension does not affect script execution; it is just for clarity of the name. If you write a shell script in PHP, just use the .php extension.

Enter some code. The first line is generally like this:

#!/bin/bash
#!/usr/bin/php

"#!" is a conventional marker that tells the system which interpreter this script needs to execute.

Running

There are two ways to run a Shell script:

There are two ways to run a Shell script:

As an executable program

chmod +x test.sh
./test.sh

Note: Be sure to write ./test.sh, not test.sh. The same applies to running other binary programs. If you directly write test.sh, the Linux system will search PATH for whether there is a file named test.sh. Only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in PATH. Your current directory is usually not in PATH, so writing test.sh will result in the command not being found. You need to use ./test.sh to tell the system to look in the current directory.

When running a Bash script this way, the first line must be written correctly so that the system can find the correct interpreter.

The "system" here is actually the shell application (imagine Windows Explorer), but I deliberately wrote "system" for ease of understanding. Since this system refers to the shell, can a script that uses /bin/sh as its interpreter omit the first line? Yes.

As an interpreter argument

This way of running is to directly run the interpreter, with its argument being the shell script file name, for example:

/bin/sh test.sh
/bin/php test.php

For scripts run in this way, there is no need to specify interpreter information on the first line; even if you write it, it is useless.


More tutorials

For more content, please refer to this site's Shell tutorial: