Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script order of execution

Tags:

bash

scripting

Do lines in a bash script execute sequentially? I can't see any reason why not, but I am really new to bash scripting and I have a couple commands that need to execute in order.

For example:

#!/bin/sh
# will this get finished before the next command starts?
./someLongCommand1 arg1
./someLongCommand2 arg1
like image 415
javamonkey79 Avatar asked Dec 15 '10 01:12

javamonkey79


People also ask

What is the Order of execution of commands in Linux?

Commands are run sequentially, but the order depends on the shell. In any case, cmd4will be executed last. In: cmd1 =(cmd2) # zsh They are executed sequentially (cmd2first). Note that in all those cases, any of those commands could start other processes. The shell would have no knowledge of them so can't possibly wait for them.

How do you execute a command in bash script?

Normally, we do not need to do anything special to execute a command inside of a Bash script. You just write the command the same way you would in your own terminal. Look at the following example where we execute three commands inside of our Bash script – echo, uptime, and who .

What is a bash script?

Bash scripts are, essentially, just a series of Linux commands that have been chained together in order to accomplish something. Depending on your code, there are a few different ways to execute commands inside the script.

How does Bash invoke commands?

The following is an excerpt of the INVOCATION section of man bash (emphasis mine): When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists.


2 Answers

Yes, they are executed sequentially. However, if you run a program in the background, the next command in your script is executed immediately after the backgrounded command is started.

#!/bin/sh
# will this get finished before the next command starts?
./someLongCommand1 arg1 &
./someLongCommand2 arg1 &

would result in an near-instant completion of the script; however, the commands started in it will not have completed. (You start a command in the background by putting an ampersand (&) behind the name.

like image 179
michiel Avatar answered Oct 20 '22 17:10

michiel


Yes... unless you go out of your way to run one of the commands in the background, one will finish before the next one starts.

like image 26
Jim Lewis Avatar answered Oct 20 '22 16:10

Jim Lewis