Home > other >  Bash function defined in .bashrc is not found within fzf calls
Bash function defined in .bashrc is not found within fzf calls

Time:08-18

I'm setting up a bash command utility based on fzf. I use some functions defined in my .bashrc to provide input to the program, as well as to handle the preview window. The problem is that within fzf, these functions do not work because they are not defined within its scope.

For example, I have this function in .basrhc:

my_fun(){
echo "hello"
}

Now when I try to use it to preview the contents selected in fzf:

fzf --preview 'my_fun {}'
/bin/bash: my_fun: order not found

The problem is pretty clear: the function is not imported within the scope of fzf. But I do not know how to work around it. How could I export it there?

CodePudding user response:

To my surprise, I could not find an existing duplicate, so posting this here.

export -f my_fun

makes the function visible to subprocesses, similarly to how export without an option makes a variable visible in the environment.

  • Related