Saturday, October 29, 2022

[SOLVED] How to call a function in a new terminal, inside a bash script?

Issue

I've written a script in which I define a function and later I call that function in a new terminal. Something like this:

#!/bin/sh

my_func(){
  echo "hello world"
  sleep 5
}

alacritty -e my_func

But I got an error :

[ERROR] [alacritty_terminal] Failed to spawn command 'my_func': No such file or directory (os error 2)

I guess that new terminal doesn`t have access to the function I've defined inside the script. How can I get around this?


Solution

alacritty -e will run a Linux command and you are giving it a function as an argument so it will not work.

The only way to make it work is splitting it into two scripts. I called them echo.sh and function.sh.

echo.sh

echo "Hello man"
sleep 5

function.sh

#!/bin/sh
alacritty -e ./echo.sh


Answered By - iker lasaga
Answer Checked By - Pedro (WPSolving Volunteer)