Shell scripting 30 minutes to get started

Source: Internet
Author: User
Tags closing tag php programming posix

What is a shell script sample

Let's look at an example:

cd ~
mkdir shell_tut
cd shell_tut

for ((i=0; i<10; i++)); do
    touch test_$i.txt
Example explanation
    • Line 1th: Specify the script interpreter, here is the interpreter with/bin/sh
    • Line 2nd: Switch to the current user's home directory
    • Line 3rd: Create a directory Shell_tut
    • Line 4th: Switch to the Shell_tut directory
    • Line 5th: Cycle conditions, 10 cycles
    • Line 6th: Create a test_1 ... 10.txt file
    • Line 7th: End of loop body

CDs, MKDIR, and touch are all programs that come with the system, typically in the/bin or/usr/bin directory. For, does, done is the keyword of the SH scripting language.

The concept of shell and shell scripting

A shell is an application that provides an interface through which users access the service of the operating system kernel. Ken Thompson's SH is the first UNIX shell,windows Explorer to be a typical graphical interface Shell.

Shell script, a scripting program written for the shell. The shell is often referred to by the industry as a shell script, but reader friends know that Shell and shell script are two different concepts. For the sake of simplicity, the "shell programming" that appears in this article refers to Shell scripting, not the development of the shell itself (such as Windows Explorer extension development).


Shell programming is like Java and PHP programming, as long as there is a text editor that can write code and a script interpreter that can explain execution.


The current mainstream operating system supports shell programming, the shell programming described in this document refers to the shell under Linux, the basic is the POSIX standard functionality, so it also applies to UNIX and BSD (such as Mac OS).


The Linux default installation brings the Shell interpreter.

Mac OS

Mac OS not only contains the two most basic interpreters, SH, bash, but also includes Ksh, CSH, zsh and other less frequently used interpreters.

Emulator on Windows

Windows does not have a built-in shell interpreter at the factory and needs to be installed on its own, in order to use grep, awk, curl and other tools, preferably a Cygwin or mingw to simulate the Linux environment.

    • Cygwin
    • MinGW
Script Interpreter SH

The Bourne shell,posix (Portable Operating System Interface) Standard shell interpreter, its binary file path is usually/bin/sh, developed by Bell Labs.

This article is about SH, if you use other languages as Shell programming, please refer to the documentation in the appropriate language.


Bash is a Bourne shell alternative, GNU Project, and the binary file path is usually/bin/bash. The industry often mixes bash, sh, and Shell, as you'll often see in the copywriting of an OPS engineer: familiar with Linux bash programming and proficient in shell programming.

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

[[email protected] ~]# 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 not on Mac OS,/bin/sh and/bin/bash are two different files, although their size is 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, as long as a language provides an interpreter (not just a compiler), the language is capable of scripting, and common explanatory languages can be used for scripting, such as Perl, TCL, Python, PHP, and Ruby. Perl is the oldest scripting language, and Python has become a preset interpreter for Linux distributions over the years.

Compiled languages, as long as there is an interpreter, can also be used as scripting, such as C shell is built-in (/BIN/CSH), Java has a third-party interpreter Jshell,ada has a charge interpreter adascript.

Here is a php Shell script example (assuming the file is named test.php):

for ($i=0; $i < 10; $i++)
        echo $i . "\n";


/usr/bin/php test.php


chmod +x test.php
How to choose the Shell programming language familiarity vs. unfamiliar

If you have mastered a programming language (such as PHP, Python, Java, JavaScript), it is recommended that you write a script directly in this language, although some may be a bit verbose, but you can take advantage of the experience in this language field (unit testing, single-step debugging, IDE, Third-party class libraries).

The new learning cost is minimal, as long as you learn how to use the Shell interpreter (Jshell, adascript).

Simple vs Advanced

If you feel familiar with the language (such as Java, C) writing shell scripts is too verbose, you just want to do some backup files, install software, download data and other things, learning to use Sh,bash will be a good idea.

The shell only defines a very simple programming language, so if your scripting program is more complex, or the data structure you want to manipulate is more complex, you should use a scripting language like Python, Perl, or a high-level language that you're already good at. Because SH and bash are weak in this regard, such as:

    • Its function can only return a string and cannot return an array
    • It doesn't support object-oriented, you can't implement some elegant design patterns
    • It is interpreted as one side of the interpretation of the execution, and even PHP that kind of precompilation is not, if your script contains errors (such as calling a nonexistent function), as long as the failure to execute this line, no error
Environmental compatibility

If your script is available to other users, using SH or bash, your script will have the best environmental compatibility, Perl is standard for Linux early on, and Python has become standard for some Linux distributions over the years, as for Mac OS, which has Perl installed by default, Python, Ruby, PHP, Java and other mainstream programming languages.

The first shell script is written

Open a text editor, create a new file, the extension sh (sh for Shell), the extension does not affect the execution of the script, see the name is good, if you write a shell script in PHP, the extension is good with PHP.

Enter some code, the first line is usually this:


“#!” is a contract tag that tells the system what interpreter the script needs to execute.


There are two ways to run a shell script:

As an executable program

chmod +x

Note, be sure to write./, not, run other binary programs as well, direct write,linux system will go to the path to find there is no, and only/bin,/sbin,/USR/BIN,/USR /sbin wait in path, your current directory is usually not in path, so write will not find the command, to use./ tells the system that it is looking in the current directory.

To run the bash script this way, the first line must be written so that the system can find the correct interpreter.

The "system" here is actually the shell application (imagine Windows Explorer), but I deliberately write the system, is easy to understand, since this system refers to the shell, then a script using/bin/sh as an interpreter can save the first line? Yes.

As an interpreter parameter

This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:

/bin/sh test.php

The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write.

Variable definition variable

When defining a variable, the variable name does not have a dollar sign ($), such as:


Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with.

In addition to explicitly assigning values directly, you can use statements to assign values to variables, such as:

for file in `ls /etc`
Using variables

With a defined variable, just precede the variable name with a dollar sign, such as:

echo $your_name
echo ${your_name}

The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:

for skill in Ada Coffe Action Java; do
    echo "I am good at ${skill}Script"

If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null) and the result of the code execution is not what we expect it to look like.

It is a good programming habit to add curly braces to all variables. IntelliJ when Idea writes Shell script, the IDE prompts for curly braces.

Redefine variables

A defined variable can be redefined, such as:

echo $your_name

echo $your_name

This is legal, but note that the second assignment can not be written $your_name= "Alibaba", the use of variables when the dollar symbol.


Lines that begin with "#" are comments, which are ignored by the interpreter.

Multi-line comments

There is no multiline comment in sh, only one # is added to each line. Just like this:

# --------------------------------------------
# This is a script that automatically starts ipa, based on webfrogs ipa-build writing:

# Function: Automatically package for etao ios app, the output is an ipa package for 14 channels
# Features: fully automatic packaging, no need to enter any parameters
# --------------------------------------------

##### User Configuration Area Start #####
# Project root directory, this script is recommended to be placed in the root directory of the project, there is no need to change it here
# Application name, make sure it matches the name under Product in Xcode
##### End of user configuration area #####

What if, in the course of development, you encounter a large segment of code that needs to be annotated temporarily and then uncomment later? Each line with a # symbol is too laborious, you can put this piece of code to be annotated with a pair of curly braces, defined as a function, there is no place to call this function, the code will not be executed, to achieve the same effect as the annotation.


Strings are the most common and useful data types in shell programming (except numbers and strings, and no other type works well, haha), strings can be in single quotes or double quotes, or without quotes. The difference between single and double quotes is similar to PHP.

Single quotation marks

str=‘this is a string‘

Single-Quote String restrictions:

    • Any character in a single quotation mark is output as is, and the variable in the single-quote string is invalid
    • Single quotation marks cannot appear in single quote string (not after using escape character for single quotes)
Double quotes

str="Hello, I know your are \"$your_name\"! \n"
    • You can have variables in double quotes.
    • Escape characters can appear in double quotes
String manipulation stitching string

greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"

echo $greeting $greeting_1
Get string Length:

string = "abcd"
echo $ {# string} #Output: 4
Extract substring

string = "alibaba is a great company"
echo $ {string: 1: 4} #Output: liba
Finding substrings

string = "alibaba is a great company"
echo `expr index" $ string "is` # Output: 8, the meaning of this sentence is: find the position of the word is in this famous phrase

See resources at the end of this document for the advanced bash-scripting Guid Chapter 10.1

Flow control of array pipeline condition judgment

Unlike Java, PHP and other languages, SH's process control is not empty, such as:

if (isset($_GET["q"])) {
else {
    //do nothing

You can't write this in Sh/bash, and if the Else branch doesn't have a statement execution, don't write this else.

Also note that if in sh [$foo-eq 0], this square bracket is very different from the parentheses in java/php if it is an executable program (like CD, LS, grep), fancy it? On CentOS, it is in the/usr/bin directory:

ll /usr/bin/[
-rwxr-xr-x. 1 root root 33408 6月  22 2012 /usr/bin/[

Just because the square brackets here is an executable program, the square brackets must be followed by a space, cannot be written as if [$foo-eq 0]

If ElseIf

if condition

Write a line (for the terminal command prompt):

if `ps -ef | grep ssh`;  then echo hello; fi

The FI at the end is if you spell it backwards, and you'll encounter a similar

If Else

if condition
If else-if Else

if condition1
elif condition2
For Whilefor

Shown in the opening example:

for var in item1 item2 ... itemN

Write a line:

for var in item1 item2 ... itemN; do command1; command2… done;
C-style for

for (( EXP1; EXP2; EXP3 ))

while condition
Infinite loops

while :


while true


for (( ; ; ))

until condition

case "${opt}" in
    "Install-Puppet-Server" )
        install_master $1

    "Install-Puppet-Client" )
        install_client $1

    "Config-Puppet-Server" )

    "Config-Puppet-Client" )

    "Exit" )

    * ) echo "Bad option, please choose again"

The syntax of the case differs greatly from the C family language, which requires a ESAC (which is case, in turn) as the closing tag, each case branch with a closing parenthesis and a two semicolon for break

The function definition call file contains

You can use the source and. Keywords, such as:

source ./ ./

In bash, source and. Are equivalent, they are read into content and execute its content (similar to PHP include), in order to better portability, it is recommended to use the second way of writing.

Include a file and execute a file, as well as write the path to the file, not to write the filename, such as the above example:

. ./

Can not write:


If is a user-passed parameter, how do you get its absolute path? The method is:

real_path = `readlink -f $ 1` # $ 1 is a parameter entered by the user, such as
. $ real_path
User input executes script when incoming script runs in input select menu stdin and stdout common commands

The SH script combines the power of a system command, in the field of character processing, with grep, awk, sed Three musketeers, grep is responsible for finding a specific line, awk can split the row into multiple fields, and SED can implement the update insert Delete write operations.


View the list of processes

grep excludes grep itself from finding results adjacent to target awksed Insert Replace delete xargscurl comprehensive case reference
    • Advanced Bash-scripting Guide, very detailed, very easy to read, a large number of example, can be used as the introductory materials, can also be consulted as reference books
    • Unix Shell Programming
    • Linux Shell Scripting tutorial-a Beginner ' s Handbook

Source Address: Https://

Shell scripting 30 minutes to get started

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.