Home  >  Article  >  Operation and Maintenance  >  How to run shell script in Linux

How to run shell script in Linux

angryTom
angryTomOriginal
2020-03-11 11:15:593777browse

How to run shell script in Linux

How to run shell scripts in Linux

There are usually three ways to execute Shell scripts. Below, we will introduce the characteristics of these three ways. :

1. bash script-name or sh script-name

This is when the script file itself does not have executable permission (that is, the x bit of the file permission attribute is -) ), or the method that needs to be used when the interpreter is not specified at the beginning of the script file. This method is recommended.

Recommended learning: Linux video tutorial

$ bash test.sh

2, path/script-name or ./script-name

means to execute the script under the current path (the script needs to have execution permission), and the permission of the script file needs to be changed to executable (that is, the file permission attribute is x bit). The specific method is: chmod a x script-name. Then you can execute the script by executing the script absolute path or relative path.

Note: In the production environment, the operation and maintenance personnel forgot to set executable permissions for the script and then used it directly, resulting in errors. Therefore, the first bash script-name is recommended.

$ chmod +x test.sh
$ ./test.sh
$ /home/me/test.sh

3. source script-name or . script-name

The function of the source or "." command is: read the script and execute the script, that is, in the current Execute source or "." in the Shell to load and execute the commands and statements of the relevant script files, instead of generating a sub-Shell to execute the commands in the file.

Note: This is the biggest difference from other ways to execute the shell.

$ source test.sh
$ . test.sh

Other operating modes:

sh test.sh
dash test.sh
zsh test.sh
...

For more related tutorials, please pay attention to PHP Chinese website!

The above is the detailed content of How to run shell script in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what does linux meanNext article:what does linux mean