I am trying to programmatically open a new terminal and automatically run a custom script that sets various environment variables to prime it for an interactive session. This all works fine, except the resulting terminal cannot be edited, i.e., there is no cursor. My current attempt looks like this:

konsole -hold -e /path/to/bash/script/run_oenv.sh

where run_oenv.sh looks like this (for simple testing):

echo hello world
export FOO=bar

When running the above command run_oenv.sh is executed successfully and I see the expected debug statement echoed in the new terminal, but I just can't work out how to make the new terminal interactive. What am I missing?

Ultimately the bash script will have to do more than just set environment variables but this is the simplest test case.

This is on Centos 7/KDE.

  • @mosvy I probably hit the spacebar by accident just before submitting the edit. Chill.
    – muru
    Jun 2 '19 at 12:47

When using -e with a terminal emulator to execute a command, it executes that command instead of the shell that it would have started. This means that it runs your script and then nothing else.

If you want to start an interactive bash shell that sources your file, use bash with its --rcfile command line option:

konsole -hold -e bash --rcfile /path/to/bash/script/run_oenv.sh

Note that this makes bash not read the ~/.bashrc file. If you still want to source ~/.bashrc, as would ordinary happen in interactive shell sessions, you would do so from your run_oenv.sh code:

[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"

or without the short-circuit syntax,

if [ -f "$HOME/.bashrc" ]; then
    source "$HOME/.bashrc"

(probably at the top of the run_oenv.sh file to have your file override any variables or settings in ~/.bashrc)

  • Thank you very much, I obviously misinterpreted the use of -e . One more complication I accidentally forgot to mention in an attempt to provide a simple example: I actually need to pass a couple of arguments to my script like this: konsole -noclose -e bash --rcfile run_oenv.sh ARG1 ARG2 How do I incorporate that? Jun 3 '19 at 20:49
  • @FrankRueter You can't really pass command line arguments to an interactive shell. But there's something else you could try, and that's to go use something like konsole -noclose -e run_oenv.sh ARG1 ARG2 where your script has a proper #!-line, and then, in the script, run bash at the end.
    – Kusalananda
    Jun 3 '19 at 21:37
  • that makes sense. Thank you,it works now with your suggestion. Jun 3 '19 at 22:07

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.