Several methods and differences for running shell scripts

Source: Internet
Author: User

#1 Add the execution permission chmod U + x a to the script. sh, and then you can directly use the full path to execute the script, for example, in the current folder. /. sh. If the directory where the script is located is in the path environment variable,. sh.ProgramIs a truth)

    #2 sh/bash./A. Sh in this case, the script does not need to have execution permissions.

    Both of the preceding methods are executed in the sub-shell. That is to say, the current Shell needs to start another shell to execute the commands in A. Sh.

     

    #1 source./A. Sh

      #2./A. Sh (note the previous .)

      The preceding two execution methods are executed in the current shell and do not need to create a sub-shell.

      Source and. Command: used to execute a script



      The difference between the first two methods and the last two methods: If you export $ KKK = 111 In a script, if you use. /. SH: run the script. After the script is executed, run echo $ KKK and find that there is no value. If you use source to execute the script and then echo it, you will find that KKK is 111. Because of the call. /. sh to execute shell is run in a sub-shell, so after execution, the structure is not reflected in the parent shell, but the source is different, it is executed in this shell, so you can see the result.


      # The script is run in a subshell environment. After the script is executed, the subshell automatically exits.

        # The system environment variables in a shell are valid only for the shell or its sub-shell. The variables disappear when the shell ends (and cannot be returned to the parent shell)

        # Variables defined with export will be copied to the sub-shell of the shell.

          # VARIABLES not defined by export are valid only for this shell and are invalid for the sub-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.