8

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 '20 at 23:17

2 Answers2

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:

pa4080
  • 29,351
  • 10
  • 85
  • 161
  • 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 '20 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 '20 at 14:43
  • the `source bin/activate` activates the [python virtual environment](https://virtualenv.pypa.io/en/stable/) 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 '20 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 '20 at 15:35
  • There is any difference if `source bin/activate && exec bash` is used instead of `source bin/activate; exec bash` ? – funder7 Aug 07 '22 at 17:13
  • Hello, @funder7, please check this answer https://askubuntu.com/a/1182653/566421 IMO, there is not difference in this case. – pa4080 Aug 07 '22 at 21:00
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
r_D
  • 141
  • 4