Home > Article > Operation and Maintenance > 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!