Shell tutorial

The Shell is a program written in C language, it is a bridge use Linux users.The Shell is a command language and a programming language.
Shell is a kind of application, the application provides an interface to the user through the interface to access the operating system kernel services.
Ken Thompson sh is the first Unix Shell, Windows Explorer is a typical GUI Shell.

A Shell script

A Shell script (Shell script), is a kind of write the script for the Shell.

Industry said shell usually refers to a shell script, but readers want to know, the shell and the shell script are two different concepts.
Because of habit, brevity, this article appeared in the “shell programming” is refers to the shell scripting, does not mean the shell itself.

Shell environment

Shell programming like JavaScript, PHP programming, as long as there is a text editor to write the code and can explain the execution of the script interpreter.

The Linux Shell type many, common are:

  • The 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 the Root (/ sbin/sh)

This tutorial focuses on the Bash, i.e., Bourne Again Shell, because is easy to use and free, Bash is widely used in daily work.At the same time, Bash is the default Shell most Linux systems.

In general, people do not distinguish between the Bourne Shell and Bourne Again Shell, therefore, like #!/ bin/sh, it can also be changed to #!/ bin/bash.

#! Called shebang, it tells the system that the program specified in the subsequent path is the Shell program that interprets the script file.

The first shell script

Open a text editor (can use vi/vim command to create the file), to create a new file test. Sh, extension of sh (sh on behalf of shell), the extension does not affect the script execution, it is good to see of knowledge meaning, if you use PHP to write a shell script, the extension in PHP.

Enter some code, the first line is usually like this:

#!/ bin/bash
Echo "Hello Ylspirit!"

#! Is a mark of the agreement, it tells the system the script to what the interpreter to execute, which USES what kind of Shell.

The echo command is used to output text to the window.

Run a Shell script has two methods:

1. as an executable program

To save the above code for the, and cd to the appropriate directory:

chmod +x ./  #make the script execute permissions

./  #execute the script

Note that must be written ./, rather than, run the other binary program, too.
Write directly, Linux system will go to the PATH for ever call, and only /bin, /sbin, /usr/bin, /usr/sbin in PATH, what do you usually not in the PATH of the current directory, so as the test. The sh would find command, want to use the / told system,In the current directory.

2. as the interpreter parameters

This operation mode is, the interpreter run directly, its parameters is a shell script file name, such as:

/bin/php test.php

This way the running script, does not require the information in the first line specifies the interpreter, wrote it would be useless.

Add a Comment

Your email address will not be published. Required fields are marked *