Introduction to shell scripting

What is Shell Scripting


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 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 shell_tut directory
  • Line 5: Loop condition, 10 loops in total
  • Line 6: Create a test_1... 10.txt file
  • Line 7: end of loop body

cd, mkdir, and touch are programs that come with the system, usually in the /bin or /usr/bin directory. for, do, done are keywords in the sh scripting language.

The concept of shell and shell scripting

A shell is an application that provides an interface through which the user accesses the services of the operating system kernel. Ken Thompson's sh was the first Unix Shell, and Windows Explorer is a typical GUI Shell.

A shell script (shell script), is a scripting program written for a shell. By shell the industry usually refers to shell scripts, but readers should know that shell and shell script are two different concepts. For reasons of convention and brevity, all references to "shell programming" in this article refer to shell scripting, not to developing the shell itself (e.g. Windows Explorer extension development).


Shell programming is the same as java and php programming, as long as you have a text editor that can write code and a script interpreter that can interpret and execute it.


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

Define the variable

Define the variabletime, Variable names without dollar signs($), as if:


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

In addition to explicitly assigning values directly, variables can be assigned values with statements such as.

for file in `ls /etc`

Use of variables

To use a defined variable, simply prefix the variable name with a dollar sign, e.g.

echo $your_name
echo ${your_name}

The brackets outside the variable name are optional, and can be added or not. They are added to help the interpreter identify the variable's bounds, as in the following case.

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

If you don't giveskill Variables in brackets, write asecho "I am good at $skillScript", The interpreter then takes the$skillScript Think of it as a variable( Its value is empty), The code execution will not be what we expect。

It is a good programming habit to recommend adding flower brackets to all variables. When IntelliJ IDEA writes a shell script, the IDE prompts to add the brackets.

attach importance to Define the variable

Variables that have been defined and can be redefined, e.g.

echo $your_name

echo $your_name

It's legal to write it that way, However, note that, The second assignment cannot be written$your_name="alibaba", Use of variables Add the dollar sign only when the。

single quote

str='this is a string'

single quote String restrictions:

  • Any characters in single quotes are output as is, and variables in single quoted strings are invalid
  • single quote cannot appear in the string single quote( right single quote Not even after using the escape character)

double quote

str="Hello, I know your are "$your_name"! 
  • You can have variables in double quotes
  • Escape characters can appear in double quotes

string operation

Splice String

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

echo $greeting $greeting_1

Gets the length of the string.

 echo ${#string} #output: 4

Extract substrings

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

Find substring

string="alibaba is a great company"
echo `expr index "$string" is`# exports:8, The meaning of this statement is: Find the wordis Position in this name

1、What does the sharing economy change in our lives and what does it bring to security companies
2、Gene Chain GHT Disrupting Medicine and Bringing about a Revolution in Genetic Science
3、Huang Renxun said NVIDIA volume exceeds AMD ten times layout of artificial intelligence or become the winner
4、Asking Blockchain Hangzhou Station A Methodology for Reconstructing Industrial Ecology in the New Era
5、Xinjiangs first regionallevel geospatial big data construction project launched

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送