A brief discussion on the differences between several ways of executing .sh files in Ubuntu

A brief discussion on the differences between several ways of executing .sh files in Ubuntu

Preface

Different script execution methods will result in different results, especially in the bash environment. There are roughly the following ways to execute a script.

First write a simple test.sh file:

#!/bin/bash

read -p "Please input your first name:" firstname
read -p "Please input your last name:" lastname
echo -e "\nYour full name is: $firstname $lastname"

Execute using sh test.sh

Use sh test.sh to execute the script file. This method indicates that the test.sh file is executed using the sh shell. sh has been replaced by bash. Although we declare in test.sh that #!/bin/bash is used to execute our file, if sh is used instead of bash, #!/bin/bash will no longer work.

Execute test.sh using bash

This method is actually the same as the principle of sh test.sh, except that we use the /bin/bash shell to execute our script file.

So, in fact, you can also use dash test.sh', it just depends on which shell you want to use to execute the script, but there are some differences among sh, bash and dash. For some keywords such as let, bash supports it, while sh and dash do not support it. For some keywords, choose to use bash.

Use point.Execute

Before using this method, you must add execution permissions to the file:

$ chmod +x test.sh

After adding the execution permission, you can use ./test.sh to execute the script. This method is the same as bash test.sh. By default, bin/bash is used to execute our script.

Only this execution method requires adding execution permissions to the file; other methods do not.

Execute using source

Using source we can also execute our script directly:

source test.sh

the difference

When we use sh test.sh, bash test.sh, and ./test.sh to execute the script, the test.sh script will use a new shell environment to execute the commands in the script. That is to say, when using these three methods, the script is actually executed in the shell of the child process. When the child process is completed, the variables and operations in the child process will end and will not be passed back to the parent process.

Can't understand? ? Take a look at the following example:

[root@ubuntu] # bash test.sh
Please input your first name: yao <==Enter first name
Please input your last name: pentonBin <==Enter lastname

Your full name is: yao pentonBin
[root@ubuntu] # echo $firstname
       <==No output here

What if you use the source method to execute the script?

[root@ubuntu] # source test.sh
Please input your first name: yao <==Enter first name
Please input your last name: pentonBin <==Enter lastname

Your full name is: yao pentonBin
[root@ubuntu] # echo $firstname
yao <== here output firstname

That is to say, the source method executes the script in the parent process, and all operations of test.sh will take effect in the original shell. This is why you need to use source ~/.bashrc instead of bash ~/.bashrc when you do not log out of the system and want to make some settings written to ~/.bashrc take effect.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the problem of passing parameters with spaces when Shell executes Python files
  • Use Linux shell script to implement FTP scheduled execution of batch download of specified files
  • Using shell scripts to automatically execute script files in Linux
  • What to do if Linux prompts No such file or directory when executing .sh file (three solutions)
  • Perfect solution for implementing "multi-threaded" script execution in Shell
  • Solution to the prompt "No such file or directory" when executing in shell script

<<:  In-depth explanation of modes and environment variables in Vue CLI

>>:  MySQL 5.7.24 installation and configuration graphic tutorial

Recommend

JS ES new features template string

Table of contents 1. What is a template string? 2...

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

Analysis of Facebook's Information Architecture

<br />Original: http://uicom.net/blog/?p=762...

How to change the tomcat port number in Linux

I have several tomcats here. If I use them at the...

Detailed explanation of Vue login and logout

Table of contents Login business process Login fu...

Vue sample code for easily implementing virtual scrolling

Table of contents Preface Rolling principle accom...

Solution to the problem that order by is not effective in MySQL subquery

By chance, I discovered that a SQL statement prod...

Specific method to add foreign key constraints in mysql

The operating environment of this tutorial: Windo...

How to Set Shortcut Icons in Linux

Preface Creating shortcuts in Linux can open appl...

Getting Started Guide to MySQL Sharding

Preface Relational databases are more likely to b...

Vue-CLI3.x automatically deploys projects to the server

Table of contents Preface 1. Install scp2 2. Conf...

Detailed explanation of custom instructions for Vue.js source code analysis

Preface In addition to the default built-in direc...

Several implementation methods of the tab bar (recommended)

Tabs: Category + Description Tag bar: Category =&...