2

I want to run a command that opens a terminal and sources into it a set of variables.

How can I do this? I'm using xfce4-terminal. It has a command-line arg "--command", to execute commands upon opening the terminal. But that arg doesn't appear to allow execution of the source command.

For example:

use terminal's --command flag to open it with sourced environment vars

xfce4-terminal \
--command 'source somefile' \
--hold &

But it just gives an error: failed to execute child process 'source': no such file or directory.

That is what you would try naturally after reading the xfce4-terminal's sparse manpage.

How about specifically invoking bash within the new terminal to source the file?

xfce4-terminal \
--command 'bash -c "source somefile \
--hold &

That executes the source command, but as soon as it is finished the terminal exits.

You might try that after searching the forums and finding it written that one must call bash to run the bash command source. (What is that? Start a terminal with a bash instance to call another bash to run the command)

call bash within bash, within bash, within terminal, to run the command

xfce4-terminal \
--command 'bash -c "source somefile; bash"' \
--hold &

That opens a terminal and keeps it open, but without the sourced environment.

Seems silly, but that's something else you might try after searching the forums.

use bash to call bash with sourced environment

xfce4-terminal \
--command 'bash -c "bash -c \"source somefile; some_command\""' \
--hold &

That sources the new env in a new terminal, but it just closes again as soon as the source cmd has finished executing. (Just some guesswork)

create a script called source_somefile which invokes bash and runs 'source somefile'

xfce4-terminal \
--command 'source_somefile' \
--hold &

No. The terminal closes again as soon as the source cmd has finished executing.

Though you can't be blamed for thinking that would surely work: putting the source cmd in a script and running that using the terminal with the --command flag, so that the --hold flag might also be used to stop the terminal exiting when the source command has finished executing?

How about, running the script as: bash -c 'source somefile'?

No: terminal closes again as soon as the source cmd has finished executing.

So how can this be done, short of printing off the xfce4-terminal manpage and ritually burning it over a blood sacrifice under a harvest moon? And then howling... howling...

0

2 Answers 2

0

You just source first and open the terminal after (I am using the portable . instead of the bash-specific source):

. source_somefile && xfce4-terminal

The terminal will inherit the environment it was sourced from. For example, if I have this:

$ cat /home/terdon/file
export my_var="foo"

And then make an alias like this:

alias my_profile='. /home/terdon/file && xfce4-terminal'

I can then run the alias which will open a new terminal in which echo $my_var will print foo. Note that the variables will need to be exported for this, and you should use the full path to the file you are sourcing.

screenshot showing this in action

1
  • Bizarrely perhaps, this did not work. For example: echo "export a=\"eh?\"" > somefile && . somefile && xfce4-terminal, left a set as expected in the controlling terminal but empty in the new one.
    – markling
    Oct 3, 2022 at 8:56
0

I've seen here a possible solution. So maybe using --disable-server flag works in the end...

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 12 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .