4 ways to execute shell scripts in Linux

Source: Internet
Author: User

This article mainly introduces the 4 methods of executing shell scripts in Linux, that is, the 4 ways to run shell scripts in Linux, and the friends you need can refer to.

There are several ways to bash a shell script, and now make a summary. Suppose we write a good shell script with the file name hello.sh, the file location in the/root/bin directory and have Execute permission (Add Permission method: chmod +x hello.sh).

method One : Switch to the directory where the shell script is located (at this time, called the working directory) to execute the shell script:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/C2/wKioL1mFbwDhELGbAAANk8eJqEA472.png "title=" Picture 1.png "alt=" Wkiol1mfbwdhelgbaaank8ejqea472.png "/>

The ./ means to execute hello.sh under the current working directory. If you do not add./,bash may respond to an error message that could not be found hello.sh. Because the current working directory (/root/bin) may not be the default search path for the executing program, that is, it is not in the contents of the environment variable pash. The Echo $PASH command is available to view the contents of the path. Now the/root/bin is not in the environment variable pash, so you must add ./ to execute.

method Two : Execute the Bash shell script as an absolute path (can be executed in the current directory or not in the current directory):

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9D/C2/wKioL1mFbxGCS4oLAAAyXX3SSIY881.png "title=" Picture 2.png "alt=" Wkiol1mfbxgcs4olaaayxx3ssiy881.png "/>

method Three : switch to the working directory (/root/bin), directly using bash or SH to execute the Bash shell script, do not give the shell script and Execute permissions:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/C2/wKiom1mFbx_j3dmZAAAog9gnhlY402.png "title=" Picture 3.png "alt=" Wkiom1mfbx_j3dmzaaaog9gnhly402.png "/>

Note : If you do this in method three, you do not have to set the shell's execution permissions beforehand, or even write the first line in the shell file (specify the bash path).

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9D/C2/wKiom1mFby7Q_9dHAAATjGscPPE495.png "title=" Picture 4.png "alt=" Wkiom1mfby7q_9dhaaatjgscppe495.png "/>

Because method three is to hello.sh as a parameter to the SH (bash) command to execute, at this time not hello.sh themselves to execute, but is called to execute, so do not execute permissions, then do not specify the bash path natural or understand AH 650) this.width= 650, "src=" Http://img.baidu.com/hi/jx2/j_0058.gif "alt=" J_0058.gif "/>650" this.width=650; "src=" http:// Img.baidu.com/hi/face/i_f47.gif "alt=" I_f47.gif "/>.

method Four : Execute in the current shell environment. hello.sh or source hello.sh to execute the Bash shell script:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9D/C2/wKioL1mFb1GBDgBNAAAiA_WJVvo864.png "title=" Picture 5.png "alt=" Wkiol1mfb1gbdgbnaaaia_wjvvo864.png "/>

Summary: The first three methods execute shell scripts in the current shell (called the parent shell) to open a child shell environment, the shell script is executed in this child shell environment, the shell script after the execution of the child shell environment is closed, And then go back to the parent shell. The method four is executed in the current shell.


4 ways to execute shell scripts in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.