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.

7
  • 3
    You're looking for source or .: $> . myscript Commented May 22, 2017 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. Commented May 22, 2017 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. Commented May 22, 2017 at 22:01
  • The environment is inherited by subshells. Commented May 22, 2017 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. Commented May 22, 2017 at 22:03

1 Answer 1

31

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Would be nice to explain also that it's actually sourcing the bash script
What is the difference between running a script with source and the answer mentioned above ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.