Shell Scripting Quick Start
Category Programming Technology
What is a Shell Script?
A Shell script (English: Shell script) is a computer program and a text file consisting of a series of shell commands that are interpreted and executed by the Unix Shell. Designed as a scripting language, it operates similarly to an interpreted language, with the Unix shell acting as a command-line interpreter. After reading the shell script, it executes the commands within it in sequence and then outputs the results. Shell scripts can be used for system management, file operations, and more.
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: Specifies the script interpreter, which is /bin/sh in this case.
- Line 2: Switches to the home directory of the current user.
- Line 3: Creates a directory named shell_tut.
- Line 4: Switches to the shell_tut directory.
- Line 5: Loop condition, loops 10 times.
- Line 6: Creates a file named test_1…10.txt.
- Line 7: End of the loop body.
Commands like cd, mkdir, and touch are system programs typically found in directories like /bin or /usr/bin. Keywords like for, do, and done are part of the sh scripting language.
Environment
Shell scripting is similar to programming in languages like Java and PHP, requiring only a text editor for writing code and a script interpreter for execution.
Mainstream operating systems support shell scripting. The shell scripting discussed in this document refers to the shell under Linux and is based on POSIX standards, making it applicable to Unix and BSD systems (such as Mac OS).
Linux
Linux comes with a shell interpreter installed by default.
Mac OS
Mac OS includes not only the basic sh and bash interpreters but also less commonly used ones like ksh, csh, and zsh.
Emulators on Windows
Windows does not come with a built-in shell interpreter and requires manual installation. To use tools like grep, awk, and curl, it's best to install an emulator like Cygwin or MinGW to simulate a Linux environment.
Script Interpreters
sh
The Bourne shell, a shell interpreter compliant with the POSIX (Portable Operating System Interface) standard, typically has a binary file path of /bin/sh, developed by Bell Labs.
bash
Bash is a replacement for the Bourne shell, part of the GNU Project, and typically has a binary file path of /bin/bash. The industry often uses bash, sh, and shell interchangeably. For example, job postings for operations engineers often state: "Familiar with Linux Bash scripting, proficient in Shell scripting."
On CentOS, /bin/sh is a symbolic link 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
However, on Mac OS, /bin/sh and /bin/bash are different files, although they are only about 100 bytes apart:
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
Advanced Programming Languages
In theory, any language that provides an interpreter (not just a compiler) can be used for scripting. Common interpreted languages suitable for scripting include Perl, Tcl, Python, PHP, and Ruby. Perl is the oldest scripting language, and Python has also become a standard interpreter in some Linux distributions.
Compiled languages can also be used for scripting if they have an interpreter, such as C shell (built-in at /bin/csh), Java with the third-party Jshell interpreter, and Ada with the commercial AdaScript interpreter.
Here 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";
}
Execution:
/usr/bin/php test.php
Or:
chmod +x test.php
./test.php
Choosing a Shell Programming Language
Familiarity vs. Novelty
If you already know a programming language (like PHP, Python, Java, JavaScript), it's recommended to use that language for scripting. Although it might be more verbose in some areas, you can leverage your experience in that language (unit testing, debugging, IDEs, third-party libraries).
The additional learning cost is minimal, just learn how to use the shell interpreter (Jshell, AdaScript).
Simplicity vs. Advanced Features
If you find that your familiar language (like Java, C) is too verbose for shell scripting, and you just want to do simple tasks like backing up files, installing software, downloading data, learning sh, bash might be a good idea.
Shell defines a very simple programming language. Therefore, if your script is complex or operates on complex data structures, you should use a scripting language like Python, Perl, or a language you are already proficient in. sh and bash are weak in these areas, for example:
- Their functions can only return strings, not arrays.
- They do not support object-oriented programming, so you cannot implement elegant design patterns.
- They are interpreted, executing line by line, not even precompiled like PHP. If your script contains errors (like calling a non-existent function), it won't report an error until that line is executed.
Environmental Compatibility
If your script is for other users, using sh or bash ensures the best environmental compatibility. Perl has been a standard on Linux for a long time, and Python has also become a standard in some Linux distributions. Mac OS comes with Perl, Python, Ruby, PHP, Java, and other mainstream programming languages by default.
Your First Shell Script
Writing
Open a text editor, create a new file with an extension of .sh (sh stands for shell). The extension does not affect script execution; it's just for clarity. If you use PHP for shell scripting, use the .php extension.
Enter some code, usually starting with:
#!/bin/bash
#!/usr/bin/php
The "#!" is a convention that tells the system which interpreter to use for executing the script.
Running
There are two ways to run a Shell script:
As an Executable Program
chmod +x test.sh
./test.sh
Note, always write ./test.sh instead of just test.sh. Running it directly will make the system look for test.sh in the PATH, and typically the current directory is not included in the PATH. Writing ./test.sh tells the system to look in the current directory.
This method requires the first line to be correct so that the system can find the correct interpreter.
As an Interpreter Argument
This method involves directly running the interpreter with the script file as an argument, like:
/bin/sh test.sh
/bin/php test.php
Scripts run this way do not need the interpreter specified in the first line; it's ignored if present.
More Tutorials
For more content, please refer to the Shell tutorials on this site:
- Shell Tutorial
- Shell Variables
- Shell Passing Arguments
- Shell Arrays
- Shell Operators
- Shell echo Command
- Shell printf Command
- Shell test Command
- Shell Flow Control
- Shell Functions
- Shell Input/Output Redirection
- Shell File Inclusion
** Click to Share Notes
-
-
-