Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a bash script, how to run bash functions defined outside?

Tags:

bash

For example, the following commands doesn't work. I wonder how to work around it, thanks.

[liuke@liuke-mbp ~]$ function showxx() { echo xx; }
[liuke@liuke-mbp ~]$ showxx
xx
[liuke@liuke-mbp ~]$ cat a.bash 
#!/bin/bash
showxx
[liuke@liuke-mbp ~]$ ./a.bash 
./a.bash: line 2: showxx: command not found
like image 609
Kan Li Avatar asked Aug 03 '11 00:08

Kan Li


1 Answers

You need to export your functions. You can either export everything when it's created (my preference) with set -a, or you can export the functions individually with export -f showxx. Either will put it into the environment, and child shells will be able to pick them up.

like image 177
evil otto Avatar answered Sep 20 '22 08:09

evil otto