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__
Results on CygPerl, Bash 5.2.21
$ 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
Results on Gnu/Linux, Bash 5.2.15
$ 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 UTC

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


In reply to Using Perl's open() to investigate bash login environment by Intrepid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.