Intrepid has asked for the wisdom of the Perl Monks concerning the following question:
After a lot of tweaking and experimentation I got code that will display for me the functions that are defined in a bash shell on my system. I first wrote the code 13 years ago. I broke this code out of a much larger modulino that I started back then and never got entirely working.
This scriptlet is working in CygPerl but not in Perl on Linux.
#!/usr/bin/env perl # Last modified: Wed Jul 16 2025 12:52:52 PM -04:00 [EDT] # Bash-functions.pl use strict; use v5.18; use utf8; use warnings; local $ENV{'PERL5SHELL'} = q=/usr/bin/bash=; local $ENV{'SHELL'} = q=/usr/bin/bash=; my $spawnFH; my $elist; my $cPid = open( $spawnFH, q[-|], qq[$ENV{'PERL5SHELL'} -login command + builtin declare -p -F] ) or die "Bad open from pipe: $!"; if ( $cPid ) { $elist = join qq[], grep {/^declare -f/} <$spawnFH>; close $spawnFH or warn qq/Bad close on process $cPid: $!|$?/; } print qq[Results from $cPid:\n], $elist, qq[\n]; __END__
$ perl Bash-functions.pl Results from 3716: declare -f SBP declare -f exp declare -f gawklibpath_append declare -f gawklibpath_default declare -f gawklibpath_prepend declare -f gawkpath_append declare -f gawkpath_default declare -f gawkpath_prepend declare -f profile_d
$ perl Bash-functions.pl /usr/bin/bash: command: No such file or directory Bad close on process 12284: |32512 at Downloads/Bash-functions.pl line + 17. Results from 12284:
Apparently bash behaves differently, when invoked this way on Linux. If I use the bash invocation alone (on Linux): command builtin declare -p -F, I see a nice list of functions. Also, I am puzzled wrt why I need to start the command with $PERL5SHELL. I would have thought, based on documentation I read, that seeing shell metacharacters in the open string would have caused perl to automatically invoke a shell.
Jul 16, 2025 at 17:46 UTCA just machine to make big decisions
Programmed by fellows (and gals) with compassion and vision
We'll be clean when their work is done
We'll be eternally free yes, and eternally young
Donald Fagen —> I.G.Y.
(Slightly modified for inclusiveness)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using Perl's open() to investigate bash login environment
by choroba (Cardinal) on Jul 16, 2025 at 20:05 UTC | |
|
Re: Using Perl's open() to investigate bash login environment
by ikegami (Patriarch) on Jul 17, 2025 at 01:20 UTC | |
|
Re: Using Perl's open() to investigate bash login environment
by eyepopslikeamosquito (Archbishop) on Jul 17, 2025 at 06:50 UTC | |
by Intrepid (Curate) on Jul 21, 2025 at 01:47 UTC |