Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading