4 Methods of executing shell script in Linux summary _linux shell

Source: Internet
Author: User

There are a number of ways to bash the shell script, now make a summary. Suppose we write a shell script with the file name hello.sh, the file location in the/data/shell directory, and already have execute permissions.

Method One: Switch to the directory where the shell script is located (at this point, called the Working directory) to execute the shell script:

Copy Code code as follows:

Cd/data/shell

./hello.sh


/means to perform hello.sh in the current working directory. If you do not add./,bash may respond to an error message that finds no hello.sh. Because the current working directory (/data/shell) may not be listed in the default search path of the executing program, that is, it is not in the content of the environment variable pash. You can view the contents of the path with the Echo $PASH command. The current/data/shell is not in the environment variable pash, so it must be added./Before it can be executed.

Method Two: Execute the Bash shell script in an absolute way:

Copy Code code as follows:

/data/shell/hello.sh

Method Three: Use bash or sh directly to execute a bash shell script:
Copy Code code as follows:

Cd/data/shell

Bash hello.sh


Or
Copy Code code as follows:

Cd/data/shell

SH hello.sh


Note that if you do this in method three, you do not have to set the shell's execution permissions beforehand, even without writing the first line in the shell file (specifying the bash path). Because method three is executed by passing the hello.sh as an argument to the SH (bash) command. This is not hello.sh yourself to execute, but is called by others to execute, so do not execute permissions. Then you don't have to specify a bash path. Naturally, ah, hehe ...

Method Four: Execute bash shell scripts in the current shell environment:

Copy Code code as follows:

Cd/data/shell

. hello.sh


Or
Copy Code code as follows:

Cd/data/shell

SOURCE hello.sh


The first three ways to execute a shell script are in the current shell (called the parent shell) to open a child shell environment, this shell script is executed in this child shell environment. After the shell script finishes, the child shell environment closes and then goes back to the parent shell. The method four is executed in the current shell.

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.