4 ways to execute Linux shell script in summary

This article describes four methods in Linux shell script summary that runs four methods in Linux shell script, a friend in need can refer to a variety of methods under bash shell script, and now make a summary. Suppose we write good shell script file named hello.sh, file location and has execute permissions in / data / shell directory.

Method One: Change to the directory where the shell script (in this case, known as the working directory) execute a shell script:

code show as below:

 

1 cd /data/shell
2 
3 ./hello.sh

 

./hello.sh

It ./ mean execution hello.sh in the current working directory. If you do not find the response plus ./,bash may not hello.sh error message. Because the current working directory (/ data / shell) program the default search path of the column may not be executed, that is, the content is not among the environmental variables PASH. View the contents of the PATH command echo $ PASH available. Now / data / shell is not in the environment variable PASH in, it must be coupled with ./ before implementation.

Method Two: The way an absolute path to execute bash shell script:

code show as below:


/data/shell/hello.sh


Method three: direct use bash or sh bash shell script to execute:

code show as below:

 

1 cd /data/shell
2 
3 bash hello.sh

 

or

code show as below:

 

1 cd /data/shell
2 
3 sh hello.sh

 

Note that if the manner of the three methods to perform, then it can be set to perform without prior permission of the shell, do not even have to write the first line (designated path bash) shell files. Third, since the method as a parameter to hello.sh sh (bash) command is executed. Then not hello.sh himself to perform, but people are called to perform, so do not execute permissions. Then do not specify the path to bash natural Ye Hao understand ah, huh, huh .......

 

Method four: Perform bash shell script in the current shell environment:

code show as below:

 

1 cd /data/shell
2 
3 . hello.sh

 

or

code show as below:

 

1 cd /data/shell
2 
3 source hello.sh

 

 

Several methods of comparison:
The first three options are open a sub-shell environment in the current shell (called the parent shell) when executing a shell script, this shell script is executed in this sub-shell environment. shell script after the implementation of the sub-shell environment closes and then went back to the parent shell. The four methods are performed in the current shell.

Guess you like

Origin www.cnblogs.com/qqfff/p/11163194.html