How to run shell script in Linux

angryTom
Release: 2020-03-11 11:15:59
Original
3883 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

Other operating modes:

sh test.sh
dash test.sh
zsh test.sh
...
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template