-2

I am new to Linux stuff and would like to know how to run command while opening the shell through /bin/bash?

Eg the steps that I want to perform:

Step1: Run the docker exec command to start the quickstart virtual machine.

$ docker exec -it 7f8c1a16e5b2 /bin/bash

Step2: The above command gives the handle of the quickstart vm on the console. Now I want to run the below command by default when ever some one starts the docker quickstart console (step 1)

cd
. ./.bash_profile

I need some guidance on how to do this. Obviously, putting all these statements in one shell script isn't helping as the commands of Step2 are to be executed in the newly opened shell (of quickstart vm). The idea is to put all these statements in a single shell script and execute it when we want to get hold of the session within the VM console.

1 Answer 1

3

You can pass the commands you want to be executed inside the container to bash with the -c option.

That would look something like this:

docker exec -it 7f8c1a16e5b2 /bin/bash -c "cd && . ./.bash_profile && /bin/bash"
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the below statement to go to the home directory (as an example). But after executing it, I didn't even get the VM console. docker exec -it 7f8c1a16e5b2 /bin/bash -c "cd"
Oh yeah I forgot if you want an interactive shell you need to add a && /bin/bash at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.