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

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
    > Apparently bash behaves differently, when invoked this way on Linux.

    I'm surprised it works in Cygwin. Reading man bash shows that you should probably use

    bash -c 'command builtin declare -p -F' # ~~

    > seeing shell metacharacters in the open string would have caused perl to automatically invoke a shell

    Yes, it causes perl to invoke a shell. But "a shell" is /bin/sh on Linux, not bash (on some systems, /bin/sh is bash in sh compatibility mode, but e.g. on Ubuntu, /bin/sh is /bin/dash).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Using Perl's open() to investigate bash login environment
by ikegami (Patriarch) on Jul 17, 2025 at 01:20 UTC

    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.

    Yes, you are executing /bin/sh (or whatever perl -V:sh returns) to execute the shell command /usr/bin/bash -login command builtin declare -p -F.

    But why? It would make more sense to execute bash directly. With the missing -c that's causing your error, we get

    open( my $spawnFH, "-|", "/usr/bin/bash", "-login", "-c" => "command builtin declare -p -F", )

    Also, it makes no sense to make the shell configurable when the command you execute is a bash command, so I have hardcoded it.

Re: Using Perl's open() to investigate bash login environment
by eyepopslikeamosquito (Archbishop) on Jul 17, 2025 at 06:50 UTC

      eyepopslikeamosquito wrote:

      I'm interested to learn the backstory behind this question. What real world problem are you trying to solve?

      I'm glad you asked. I don't know how "real" my world is ;-) but the quick answer is that I want to fashion a Perl program to save and display the entire ENV of the current process as YAML. I am just fascinated by the operating system's management of forked or spawned child processes and the way in which these child processes inherit an environment from the parent. In particular, this interests me on Windows with Cygwin, when some of the ENV comes from Cygwin initialization and some from the "larger" Windows system ENV. In real-world situations, sometimes the ENV contains contradictory or troublesome entries that I want to know about, so I can perform knowledgeable systems management and good defensive programming.

      Since you mention Cygwin, is your code required to run on both Windows and Unix?

      Hmm. Well, at present my ambitions are limited to having my code run on Cygwin, and not on unix in general. That could change if I can get a handle on preparing data to Load() and then Dump() with the YAML module (actually the implementation I am using is YAML::XS).

      Thanks for the references to nodes you felt related to this topic. I will read them soon.

          - Soren

      Jul 21, 2025 at 01:47 UTC