0

I am trying to write a bash script that opens a new gnome terminal window and runs an aliased command. I have seen that instead of using aliases using a function is the proper way to go. I have tried both approaches but I can't get it to work though, as I am getting an error that the command is not recognized.

This is what I have.

#!/bin/bash

# Cluster related functions
function gateway () {
    ssh user@something -t ssh gateway
}

gnome-terminal --window-with-profile=Bash -- bash -c "gateway; bash;"

The error I'm getting is:

bash: gateway: command not found

1 Answer 1

0

The simplest solution is to export the function before calling gnome-terminal:

export -f gateway

Notes:

  • You cannot export aliases.

  • Exporting functions is not a portable feature. It will work from bash to bash.

  • It will naturally work if the new bash is a descendant of the old bash (where the function has been exported). E.g. if gnome-terminal inherits from the old bash and the new bash inherits from the gnome-terminal. This happens when the old bash runs in a terminal that is not gnome-terminal.

    My tests indicate if gnome-terminal runs from within gnome-terminal (so the environment contains GNOME_TERMINAL_* variables) then the new gnome-terminal process will delegate the task to the already running gnome-terminal and exit. The natural inheritance of the environment to the new bash cannot occur, still the new gnome-terminal process is smart enough to communicate not only the command but also the environment. This way the new bash inside the old gnome-terminal behaves like a descendant of the old bash, the exported function works. Tested with gnome-terminal 3.28.2.

You must log in to answer this question.

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