Is it possible to call bash function from Perl?

admin@admin:~$ function fun1() { echo "abc"; }
admin@admin:~$ fun1
abc

I want to call this function from Perl.

admin@admin:~$ perl -e 'fun1'
admin@admin:~$ 

There is no output. What do I need to do to run my shell functions and other variables or Perl isn’t able to do this?

>Solution :

A function can be exported in bash, then called in a subshell

$ function fun1 { echo "abc"; }
$ export -f fun1
$ perl -wE'system "bash", "-c", "fun1"'

(where $ signifies a shell prompt)

Tested in bash 5.1.8(1)

Leave a Reply