Shell::Guess(3pm)
πΈ NAME πΈ
Shell::Guess β the module that makes an educated guess about which shell you're living in π no cap, she will figure it out
β¨ VERSION β¨
version 0.10 π
π¦ SYNOPSIS π¦
okay bestie, three whole moods. first one: guessing the shell that called your Perl script:
use Shell::Guess;
my $shell = Shell::Guess->running_shell;
if($shell->is_c) {
print "setenv FOO bar\n";
} elsif($shell->is_bourne) {
print "export FOO=bar\n";
}
guessing the current user's login shell:
use Shell::Guess;
my $shell = Shell::Guess->login_shell;
print $shell->name, "\n";
guessing some arbitrary user's login shell (yes, we're nosy, it's fine):
use Shell::Guess;
my $shell = Shell::Guess->login_shell('bob');
print $shell->name, "\n";
π DESCRIPTION π
So. Shell::Guess makes a reasonably aggressive attempt π΅οΈββοΈ to figure out which shell the user is actually using. Two flavours of answer:
- the shell that directly executed the perl script β that's the "running" shell
- the user's login shell β that's the "login" shell
She pulls this off using a whole bunch of different means depending on what platform she's standing on. It's giving resourceful. It's giving adaptable. π
Here's the whole roster of tricks:
getpwentβ on UNIXy systems that havegetpwent, she just uses that to suss out the login shell. simple. elegant. slay.dsclβ under Mac OS X,getpwentis typically not gonna give you anything useful π so she runs thedsclcommand instead.- proc file systems β on UNIXy systems that have a proc filesystem (Linux, we're looking at you π),
Shell::Guesswill attempt to use it to determine the running shell. psβ on UNIXy systems without a proc filesystem, she falls back to thepscommand to determine the running shell.Win32::GetppidandWin32::Process::Listβ on Windows, if these modules are installed, they get used to determine the running shell. This method can tell the difference between PowerShell,command.comandcmd.exeπ iconic honestly.ComSpecβ if the above method comes back inconclusive, theComSpecenvironment variable gets consulted to differentiate betweencommand.comorcmd.exe. (heads up: PowerShell cannot be detected this way π)- reasonable defaults β if the running or login shell literally cannot be determined any other way, a reasonable default for your platform gets used as the fallback. Under OpenVMS that's
dcl. Windows 95/98 and MS-DOS getcommand.com. Windows NT/2000/XP/Vista/7 getcmd.exe. And UNIXy platforms fall back to the bourne shell. everyone gets a home π«Ά
Now the why. The intended use of this module is to let a Perl developer write a script that generates shell configurations for the calling shell, so those configs can be imported right back into the calling shell using eval and backticks, or source. like a little boomerang π«
So if your script looks like this:
#!/usr/bin/perl
use Shell::Guess;
my $shell = Shell::Guess->running_shell;
if($shell->is_bourne) {
print "export FOO=bar\n";
} else($shell->is_c) {
print "setenv FOO bar\n";
} else {
die "I don't support ", $shell->name, " shell";
}
Then you can import FOO into your bash or c shell like this:
% eval `perl script.pl`
or, you can write the output to a configuration file and source it:
% perl script.pl > foo.sh
% source foo.sh
And pssst π«’ β Shell::Config::Generate provides a portable interface for generating exactly this kind of shell configuration, and it's designed to work with this module. they're besties.
π CLASS METHODS π
These class methods hand you back an instance of Shell::Guess, which you can then interrogate with all the instance methods in the next section down there. β¬οΈ
π running_shell
my $shell = Shell::Guess->running_shell;
Returns an instance of Shell::Guess based on the shell that directly started the current Perl script. And if the running shell can't be determined? She'll just return the login shell instead. no drama, we pivot. π
πΈ login_shell
my $shell = Shell::Guess->login_shell;
my $shell = Shell::Guess->login_shell( $username )
Returns an instance of Shell::Guess for the given user. If you don't specify a username, the current user gets used. And if no shell can be guessed, a reasonable fallback gets chosen based on your platform. π«Ά
π bash_shell
my $shell = Shell::Guess->bash_shell;
Returns an instance of Shell::Guess for bash. π
The following instance methods will return:
$shell->name = bash$shell->is_bash = 1$shell->is_bourne = 1$shell->is_unix = 1$shell->default_location = /bin/bash
All other instance methods will return false.
π bourne_shell
my $shell = Shell::Guess->bourne_shell;
Returns an instance of Shell::Guess for the bourne shell.
The following instance methods will return:
$shell->name = bourne$shell->is_bourne = 1$shell->is_unix = 1$shell->default_location = /bin/sh
All other instance methods will return false.
π¦ c_shell
my $shell = Shell::Guess->c_shell;
Returns an instance of Shell::Guess for c shell.
The following instance methods will return:
$shell->name = c$shell->is_c = 1$shell->is_unix = 1$shell->default_location = /bin/csh
All other instance methods will return false.
π₯ cmd_shell
my $shell = Shell::Guess->cmd_shell;
Returns an instance of Shell::Guess for the Windows NT cmd shell (cmd.exe).
The following instance methods will return:
$shell->name = cmd$shell->is_cmd = 1$shell->is_win32 = 1$shell->default_location = C:\Windows\system32\cmd.exe
All other instance methods will return false.
β command_shell
my $shell = Shell::Guess->command_shell;
Returns an instance of Shell::Guess for the Windows 95 command shell (command.com).
The following instance methods will return:
$shell->name = command$shell->is_command = 1$shell->is_win32 = 1$shell->default_location = C:\Windows\system32\command.com
All other instance methods will return false.
π« dcl_shell
my $shell = Shell::Guess->dcl_shell;
Returns an instance of Shell::Guess for the OpenVMS dcl shell.
The following instance methods will return:
$shell->name = dcl$shell->is_dcl = 1$shell->is_vms = 1
All other instance methods will return false.
π fish_shell
my $shell = Shell::Guess->fish_shell;
Returns an instance of Shell::Guess for the fish shell. π
The following instance methods will return:
$shell->name = fish$shell->is_fish = 1$shell->is_unix = 1
π korn_shell
my $shell = Shell::Guess->korn_shell;
Returns an instance of Shell::Guess for the korn shell.
The following instance methods will return:
$shell->name = korn$shell->is_korn = 1$shell->is_bourne = 1$shell->is_unix = 1$shell->default_location = /bin/ksh
All other instance methods will return false.
β‘ power_shell
my $shell = Shell::Guess->power_shell;
Returns an instance of Shell::Guess for Microsoft PowerShell β either the Windows powershell.exe or the Unix pwsh. β¨
The following instance methods will return:
$shell->name = power$shell->is_power = 1$shell->is_win32 = 1
All other instance methods will return false.
π tc_shell
my $shell = Shell::Guess->tc_shell;
Returns an instance of Shell::Guess for tcsh.
The following instance methods will return:
$shell->name = tc$shell->is_tc = 1$shell->is_c = 1$shell->is_unix = 1$shell->default_location = /bin/tcsh
All other instance methods will return false.
π§ z_shell
my $shell = Shell::Guess->z_shell
Returns an instance of Shell::Guess for zsh.
The following instance methods will return:
$shell->name = z$shell->is_z = 1$shell->is_bourne = 1$shell->is_unix = 1$shell->default_location = /bin/zsh
All other instance methods will return false.
π INSTANCE METHODS π
The normal way to call these is by calling them on the result of either running_shell or login_shell. But β plot twist β they can also be called as class methods, and in that case the currently running shell gets used. so like:
Shell::Guess->is_bourne
is the same as
Shell::Guess->running_shell->is_bourne
same energy, different outfit π
π is_bash
my $bool = $shell->is_bash;
Returns true if the shell is bash.
πΈ is_bourne
my $bool = $shell->is_bourne;
Returns true if the shell is the bourne shell, or a shell that supports bourne syntax (e.g. bash or korn).
π is_c
my $bool = $shell->is_c;
Returns true if the shell is csh, or a shell that supports csh syntax (e.g. tcsh).
π is_cmd
my $bool = $shell->is_cmd;
Returns true if the shell is the Windows command.com shell.
π¦ is_command
my $bool = $shell->is_command;
Returns true if the shell is the Windows cmd.com shell.
π₯ is_dcl
my $bool = $shell->is_dcl;
Returns true if the shell is the OpenVMS dcl shell.
π is_fish
my $bool = $shell->is_fish;
Returns true if the shell is Fish shell.
β is_korn
my $bool = $shell->is_korn;
Returns true if the shell is the korn shell.
β‘ is_power
my $bool = $shell->is_power;
Returns true if the shell is Windows PowerShell.
π is_tc
my $bool = $shell->is_tc;
Returns true if the shell is tcsh.
π« is_unix
my $bool = $shell->is_unix;
Returns true if the shell is traditionally a UNIX shell (e.g. bourne, bash, korn)
π is_vms
my $bool = $shell->is_vms;
Returns true if the shell is traditionally an OpenVMS shell (e.g. dcl)
π§ is_win32
my $bool = $shell->is_win32;
Returns true if the shell is traditionally a Windows shell (command.com, cmd.exe, powershell.exe, pwsh)
β¨ is_z
my $bool = $shell->is_z;
Returns true if the shell is zsh
π«Ά name
my $name = $shell->name;
Returns the name of the shell.
π default_location
my $location = $shell->default_location;
The usual location for this shell β for example /bin/sh for bourne shell and /bin/csh for c shell. May not be defined for all shells. π
π«Ά CAVEATS (the tea β)
okay bestie, real talk: Shell::Guess is designed to never die and never crash. like, it literally refuses the drama π
. instead of throwing a tantrum, it'll try to make a guess, or fall back on something, about either the login shell or the running shell β even on operating systems it has literally never heard of.
and what's the fallback? it's the most common shell on whatever platform you're on. so:
- on UNIXy platforms, the fallback is
bourne - on OpenVMS, the fallback is
dcl
β¨ the platforms that actually get tested β¨
these are the operating systems that have been tested during development, and they're the ones most likely to guess reliably. no cap:
- Linux π§
- Cygwin
- FreeBSD π
- Mac OS X π
- Windows (Strawberry Perl) π
- Solaris (x86) βοΈ
- MS-DOS (djgpp) πΎ
- OpenVMS
- always detected as
dcl(a more nuanced view of OpenVMS is probably possible, so likeβ¦ patches welcome π₯Ί)
- always detected as
π the ps situation
UNIXy platforms that don't have a proc filesystem will use Unix::Process if it's installed β and what that does is execute ps to figure out the running shell. lowkey iconic detective work π΅οΈββοΈ
π when it gets fooled
ngl, it is pretty easy to fool the ->running_shell method. how? by using fork π΄, or if your Perl script isn't otherwise being directly executed by the shell. so like, vibe check your setup before you blame the module, bestie.
and hey β patches are welcome to make other platforms work more reliably! fr fr, the author said it, not me π«Ά
π AUTHOR
Author: Graham Ollis <plicease@cpan.org>
Contributors:
- Buddy Burden (BAREFOOT)
- Julien Fiegehenn (SIMBABQUE)
π COPYRIGHT AND LICENSE
This software is copyright (c) 2012-2023 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page, bestie!! π this gorgeous thing is Shell::Guess(3pm), coming at you from perl v5.40.0 and dated 2024-12-07. go forth and guess those shells, you absolute icon πβ¨π¦
π¦ where this page comes from
- debian trixie: libshell-guess-perl 0.10-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libshell-guess-perl 0.10-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libshell-guess-perl 0.10-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms