Three ways to execute shell scripts

Source: Internet
Author: User

Shell scripts can be executed in the following ways:

Way One:

./script.sh     #  using a decimal point to perform

Way two:

SH script.sh or bash script.sh      #   using bash (SH) to execute scripts

The previous two methods are all the same: they are executed in a child process under the current parent process, and when the child process is complete, the variables or actions in the child process will end without being passed back to the parent process. For example:

As you can see, after execution, look at the two variables in the script that don't work in the parent process!

Note: If the script1.sh script is executed inside the script2.sh file, it is executed. ./script1.sh [There are spaces in the middle]

Way three:

SOURCE script.sh  or. liqiang.sh#   uses a decimal point to perform

The third way of executing (source test.sh) is that it works in the parent process:

This is the difference between direct execution and execution with the source command, which applies only to the child process itself, while the latter acts on the entire parent process.

Therefore: If you want to not unregister the system and have the global configuration file take effect, you must use the source command:

For example: In the global configuration file/etc/profile added Java_home, let him for the entire environment to take effect

Export java_home=/usr/java/jdk1.7.0_75

You have to do source/etc/profile.

Note: To assign permissions at execution time

chmod a+x liqiang.sh #赋权限chmod a+x script-name

Complementary points of knowledge:

A canonical shell script indicates in the first line which program (interpreter) to execute the contents of the script, which is typically programmed in Linux bash:

#! /bin/bash

Or

#! /bin/sh

Attention:

(1) In the shell, if the first letter of a line is #, then it is a comment, but the above two are written on the first line, so it is not a script comment line, and if it is written after a command, it becomes a comment line.

(2) SH is a soft link for bash, in most cases, the beginning of the script using "#!/bin/bash" and "#!/bin/sh" is no different, but the more canonical notation is to use "#!/bin/bash" at the beginning of the script.

Three ways to execute shell scripts

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.