7

I need a bash script that does the following:

  1. open a new terminal
  2. change to a specific directory
  3. run a command in that directory
  4. keep the terminal open for further use

Specifically I want to:

  1. open the konsole terminal
  2. change to /my/work/dir/
  3. inside /my/work/dir/, run source bin/activate
  4. after that I need to run further commands inside /my/work/dir/, e.g. ls

A very similar question was given the following answer for the script (adapted to my requirements):

#!/usr/bin/env bash
konsole --noclose --workdir /my/work/dir/ -e 'bash -c 'source bin/activate'' 

This does open a new terminal inside /my/work/dir/, but the terminal is not interactive. The

user@userMachine: /my/work/dir$

is missing that allows me to run further commands and anything I type (e.g. pwd) returns nothing, just new lines.

  • Shells like bash have the concept of rcfiles, which are essentially sourced at the start of the shell session. Could you accomplish what you want by defining a custom rcfile and creating a konsole profile that triggers the shell with that rcfile? – kojiro Feb 13 at 23:17
7

You need to specify what the terminal to do after executing the command source bin/activate. You want an active bash session so you need to run bash. In addition there is a mes with the quotes. So the script could be:

#!/usr/bin/env bash
konsole --noclose --workdir /my/work/dir/ -e 'bash -c "source bin/activate; exec bash"' &
  • & at the end is added in order to keep the main terminal usable, in case you are executing that script from other terminal window.

  • the exec command could be omitted, and you can use only bash.

Here are few answers of similar questions, dedicated to gnome-terminal:

| improve this answer | |
  • Ok This shows some strange behaviour now: The terminal opens in the correct directory. Also the virtualenv seems to be running, since pip freeze gives only the packages installed in the virtualenv. But when I then try to $ deactivate i get deactivate: command not found. I then activate again (again pip freeze gives correct output), then deactivate, and now pip freeze STILL gives the environment packages, not the system wide packages as expected... – Douglas James Bock Feb 13 at 14:38
  • Hello, @DouglasJamesBock, I really do not have an idea how your environment is setup and what bin/activate actually does. Try to change the shebang of the script to #!/bin/bash and/or source also the $HOME/.bashrc file probably some paths are loaded by this file. – pa4080 Feb 13 at 14:43
  • the source bin/activate activates the python virtual environment I previously created in my/work/dir. I have also tried your other suggestions without improvements. I can not even exit the terminal, I guess due to the --noclose. I'm starting to think that Konsole might be to limited for this. – Douglas James Bock Feb 13 at 15:02
  • @DouglasJamesBock: Please tray with some other terminal emulator, gnomen-terminal I would say - check the references given in the answer. – pa4080 Feb 13 at 15:35
3

I am not able to comment, but from what I understand I may suggest

#!/bin/bash
cd /my/work/dir/
source bin/activate
konsole
| improve this answer | |
  • This would make the most sense to me! Set up the environment, then launch konsole with the environment set. – Cinderhaze Feb 13 at 18:55
  • This gives the same issues as my first comment under pa4080's answer. – Douglas James Bock Feb 13 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.