10

I have a bash script which is calling three different commands and that execution must happen in one shell. I got it by adding && after each command like as follows-

CMD1 && CMD2 && CMD3

Now what i need is- lets say i open a terminal on my MAC machine, all commands should run in open shell not in new sub-shell.

As a side note- CMD1 is actually a source command to my project directory which is a bash script which sets all the environment variable for running server.

Sam
  • 859
  • 3
  • 12
  • 23
  • 3
    You're looking for `source` or `.`: `$> . myscript` – Kyle Strand May 22 '17 at 21:57
  • thanks @KyleStrand actually my CMD1 is itself a source command. That is why i am looking for running bash script in current shell. Because CMD2 and CMD3 depends on environment set by CMD1. – Sam May 22 '17 at 22:00
  • ...then your question is unclear; commands aren't intrinsically "source commands". Do you mean that your script actually looks like `. CMD1 && CMD2 && CMD3`? If so, your question should say that. – Kyle Strand May 22 '17 at 22:01
  • The environment is inherited by subshells. – William Pursell May 22 '17 at 22:03
  • 1
    Please edit your question to clarify what your script looks like and what your requirements are. "CMD2 and CMD3 depends on environment set by CMD1" should be specified in the *question itself*, not in a comment. – Kyle Strand May 22 '17 at 22:03
  • It's also unclear what you mean by "execution must be in sequence". Bash execution is *always* in-sequence, except when you start a new Bash shell as a background process (which is not likely to be something you'd do accidentally). `&&` has the additional property of *halting* execution of commands as soon as one of them has a non-zero return value. – Kyle Strand May 22 '17 at 22:06
  • @KyleStrand Edited the question. Hope its clear now. – Sam May 22 '17 at 22:11

1 Answers1

30

First, you will need to save save the command in a scriptfile, for example Myscript.sh.

Second, you can execute the script file to run your commands simultaneously using

. ./Myscript.sh

The first . stands for current shell and the second . for current directory.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Nikhil Fadnis
  • 787
  • 5
  • 14