One-click: Open terminal, run commands, keep using the terminal
Solution 1
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 onlybash
.
Here are few answers of similar questions, dedicated to gnome-terminal
:
Solution 2
I am not able to comment, but from what I understand I may suggest
#!/bin/bash
cd /my/work/dir/
source bin/activate
konsole
Related videos on Youtube
Douglas James Bock
Undergrad majoring in Physics and Mathematics. Constantly attempting to cube my spherical cow.
Updated on September 18, 2022Comments
-
Douglas James Bock almost 2 years
I need a bash script that does the following:
- open a new terminal
- change to a specific directory
- run a command in that directory
- keep the terminal open for further use
Specifically I want to:
- open the konsole terminal
- change to
/my/work/dir/
- inside
/my/work/dir/
, runsource bin/activate
- 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. Theuser@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. -
Douglas James Bock over 4 yearsOk 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 getdeactivate: command not found
. I then activate again (again pip freeze gives correct output), then deactivate, and nowpip freeze
STILL gives the environment packages, not the system wide packages as expected... -
pa4080 over 4 yearsHello, @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/orsource
also the$HOME/.bashrc
file probably some paths are loaded by this file. -
Douglas James Bock over 4 yearsthe
source bin/activate
activates the python virtual environment I previously created inmy/work/dir
. I have also tried your other suggestions without improvements. I can not evenexit
the terminal, I guess due to the--noclose
. I'm starting to think that Konsole might be to limited for this. -
pa4080 over 4 years@DouglasJamesBock: Please tray with some other terminal emulator,
gnomen-terminal
I would say - check the references given in the answer. -
Cinderhaze over 4 yearsThis would make the most sense to me! Set up the environment, then launch konsole with the environment set.
-
Douglas James Bock over 4 yearsThis gives the same issues as my first comment under pa4080's answer.