Ubuntu – One-click: Open terminal, run commands, keep using the terminal

bashcommand linekonsole

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.

Best Answer

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: