4 ways to execute the shell script and the difference introduction

Source: Internet
Author: User
Tags inheritance relative

This article mainly introduces 4 methods of executing the shell script and the difference introduction, this article explained the relative path way, the absolute path way, the Bash command call, the relative or the absolute way as well as the next several ways difference, needs the friend to be possible to refer to under

There are several ways to execute a shell script

1, relative path mode, need to first CD to script path

The code is as follows:

[Root@banking tmp]# Cd/tmp

[Root@banking tmp]#./ceshi.sh

Script execution succeeded

2. Absolute path mode

The code is as follows:

[Root@banking tmp]#/tmp/ceshi.sh

Script execution succeeded

3. Bash Command call

The code is as follows:

[Root@banking/]# bash/tmp/ceshi.sh

Script execution succeeded

4,. (space) A relative or absolute way

The code is as follows:

[Root@banking/]#. /tmp/ceshi.sh

The difference of several ways

There is no difference between the first and the second, both of which require a script to execute permissions in advance.

The third is to handle the script as a call to bash, so the script does not need execute permissions to execute.

The first three ways are to open a child shell in the current shell to execute the script content, and when the content of the script ends, the child shell closes and goes back to the parent shell.

The fourth is to make the content of the script execute in the current shell instead of opening the child shell alone.

The difference between the open shell and the open shell is that the inheritance of the environment variables, such as the current variable set in the child shell, does not make special channel processing, the parent shell is not visible.

When executed in the current shell, all set environment variables are valid for immediate use.

Verify:

The code is as follows:

[Root@banking/]# cat/tmp/ceshi.sh

Top

1, the first three kinds of execution mode under the Pstree display

The code is as follows:

├─sshd─┬─sshd───bash───bash───top

│└─sshd───bash───pstree

2, the fourth way of implementation of the Pstree display

The code is as follows:

├─sshd─┬─sshd───bash───top

│└─sshd───bash───pstree

3, verify the environment variable settings of the inheritance and visibility relationships

Create two scripts, father.sh and subshell.sh. where father.sh calls subshell.sh

The code is as follows:

[Root@banking/]# cat/tmp/father.sh

V_ceshi= ' father '

#-------Define variables in the parent shell

echo "Invoke script as a child shell"

/tmp/subshell.sh

echo "Output V_ceshi value is ${v_ceshi}"

echo ""

echo "Execute script in current Shell"

. /tmp/subshell.sh

echo "Output V_ceshi value is ${v_ceshi}"

[Root@banking/]#

[Root@banking/]# cat/tmp/subshell.sh

V_ceshi=son

[Root@banking/]#

Execution results are

The code is as follows:

[Root@banking/]#/tmp/father.sh

Calling script as a child shell

Output V_ceshi value is Father

Executing scripts in the current shell

Output V_ceshi value is son

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.