1Shell::Guess(3) User Contributed Perl Documentation Shell::Guess(3)
2
3
4
6 Shell::Guess - Make an educated guess about the shell in use
7
9 version 0.09
10
12 guessing shell which called the Perl script:
13
14 use Shell::Guess;
15 my $shell = Shell::Guess->running_shell;
16 if($shell->is_c) {
17 print "setenv FOO bar\n";
18 } elsif($shell->is_bourne) {
19 print "export FOO=bar\n";
20 }
21
22 guessing the current user's login shell:
23
24 use Shell::Guess;
25 my $shell = Shell::Guess->login_shell;
26 print $shell->name, "\n";
27
28 guessing an arbitrary user's login shell:
29
30 use Shell::Guess;
31 my $shell = Shell::Guess->login_shell('bob');
32 print $shell->name, "\n";
33
35 Shell::Guess makes a reasonably aggressive attempt to determine the
36 shell being employed by the user, either the shell that executed the
37 perl script directly (the "running" shell), or the users' login shell
38 (the "login" shell). It does this by a variety of means available to
39 it, depending on the platform that it is running on.
40
41 • getpwent
42
43 On UNIXy systems with getpwent, that can be used to determine the
44 login shell.
45
46 • dscl
47
48 Under Mac OS X getpwent will typically not provide any useful
49 information, so the dscl command is used instead.
50
51 • proc file systems
52
53 On UNIXy systems with a proc filesystems (such as Linux),
54 Shell::Guess will attempt to use that to determine the running
55 shell.
56
57 • ps
58
59 On UNIXy systems without a proc filesystem, Shell::Guess will use
60 the ps command to determine the running shell.
61
62 • Win32::Getppid and Win32::Process::List
63
64 On Windows if these modules are installed they will be used to
65 determine the running shell. This method can differentiate between
66 PowerShell, "command.com" and "cmd.exe".
67
68 • ComSpec
69
70 If the above method is inconclusive, the ComSpec environment
71 variable will be consulted to differentiate between "command.com"
72 or "cmd.exe" (PowerShell cannot be detected in this manner).
73
74 • reasonable defaults
75
76 If the running or login shell cannot be otherwise determined, a
77 reasonable default for your platform will be used as a fallback.
78 Under OpenVMS this is dcl, Windows 95/98 and MS-DOS this is
79 command.com and Windows NT/2000/XP/Vista/7 this is cmd.exe. UNIXy
80 platforms fallback to bourne shell.
81
82 The intended use of this module is to enable a Perl developer to write
83 a script that generates shell configurations for the calling shell so
84 they can be imported back into the calling shell using "eval" and
85 backticks or "source". For example, if your script looks like this:
86
87 #!/usr/bin/perl
88 use Shell::Guess;
89 my $shell = Shell::Guess->running_shell;
90 if($shell->is_bourne) {
91 print "export FOO=bar\n";
92 } else($shell->is_c) {
93 print "setenv FOO bar\n";
94 } else {
95 die "I don't support ", $shell->name, " shell";
96 }
97
98 You can then import FOO into your bash or c shell like this:
99
100 % eval `perl script.pl`
101
102 or, you can write the output to a configuration file and source it:
103
104 % perl script.pl > foo.sh
105 % source foo.sh
106
107 Shell::Config::Generate provides a portable interface for generating
108 such shell configurations, and is designed to work with this module.
109
111 These class methods return an instance of Shell::Guess, which can then
112 be interrogated by the instance methods in the next section below.
113
114 running_shell
115 my $shell = Shell::Guess->running_shell;
116
117 Returns an instance of Shell::Guess based on the shell which directly
118 started the current Perl script. If the running shell cannot be
119 determined, it will return the login shell.
120
121 login_shell
122 my $shell = Shell::Guess->login_shell;
123 my $shell = Shell::Guess->login_shell( $username )
124
125 Returns an instance of Shell::Guess for the given user. If no username
126 is specified then the current user will be used. If no shell can be
127 guessed then a reasonable fallback will be chosen based on your
128 platform.
129
130 bash_shell
131 my $shell = Shell::Guess->bash_shell;
132
133 Returns an instance of Shell::Guess for bash.
134
135 The following instance methods will return:
136
137 • $shell->name = bash
138
139 • $shell->is_bash = 1
140
141 • $shell->is_bourne = 1
142
143 • $shell->is_unix = 1
144
145 • $shell->default_location = /bin/bash
146
147 All other instance methods will return false
148
149 bourne_shell
150 my $shell = Shell::Guess->bourne_shell;
151
152 Returns an instance of Shell::Guess for the bourne shell.
153
154 The following instance methods will return:
155
156 • $shell->name = bourne
157
158 • $shell->is_bourne = 1
159
160 • $shell->is_unix = 1
161
162 • $shell->default_location = /bin/sh
163
164 All other instance methods will return false
165
166 c_shell
167 my $shell = Shell::Guess->c_shell;
168
169 Returns an instance of Shell::Guess for c shell.
170
171 The following instance methods will return:
172
173 • $shell->name = c
174
175 • $shell->is_c = 1
176
177 • $shell->is_unix = 1
178
179 • $shell->default_location = /bin/csh
180
181 All other instance methods will return false
182
183 cmd_shell
184 my $shell = Shell::Guess->cmd_shell;
185
186 Returns an instance of Shell::Guess for the Windows NT cmd shell
187 (cmd.exe).
188
189 The following instance methods will return:
190
191 • $shell->name = cmd
192
193 • $shell->is_cmd = 1
194
195 • $shell->is_win32 = 1
196
197 • $shell->default_location = C:\Windows\system32\cmd.exe
198
199 All other instance methods will return false
200
201 command_shell
202 my $shell = Shell::Guess->command_shell;
203
204 Returns an instance of Shell::Guess for the Windows 95 command shell
205 (command.com).
206
207 The following instance methods will return:
208
209 • $shell->name = command
210
211 • $shell->is_command = 1
212
213 • $shell->is_win32 = 1
214
215 • $shell->default_location = C:\Windows\system32\command.com
216
217 All other instance methods will return false
218
219 dcl_shell
220 my $shell = Shell::Guess->dcl_shell;
221
222 Returns an instance of Shell::Guess for the OpenVMS dcl shell.
223
224 The following instance methods will return:
225
226 • $shell->name = dcl
227
228 • $shell->is_dcl = 1
229
230 • $shell->is_vms = 1
231
232 All other instance methods will return false
233
234 fish_shell
235 my $shell = Shell::Guess->fish_shell;
236
237 Returns an instance of Shell::Guess for the fish shell.
238
239 The following instance methods will return:
240
241 • $shell->name = fish
242
243 • $shell->is_fish = 1
244
245 • $shell->is_unix = 1
246
247 korn_shell
248 my $shell = Shell::Guess->korn_shell;
249
250 Returns an instance of Shell::Guess for the korn shell.
251
252 The following instance methods will return:
253
254 • $shell->name = korn
255
256 • $shell->is_korn = 1
257
258 • $shell->is_bourne = 1
259
260 • $shell->is_unix = 1
261
262 • $shell->default_location = /bin/ksh
263
264 All other instance methods will return false
265
266 power_shell
267 my $shell = Shell::Guess->power_shell;
268
269 Returns an instance of Shell::Guess for Microsoft PowerShell (either
270 for Windows "powershell.exe" or Unix "pwsh").
271
272 The following instance methods will return:
273
274 • $shell->name = power
275
276 • $shell->is_power = 1
277
278 • $shell->is_win32 = 1
279
280 All other instance methods will return false
281
282 tc_shell
283 my $shell = Shell::Guess->tc_shell;
284
285 Returns an instance of Shell::Guess for tcsh.
286
287 The following instance methods will return:
288
289 • $shell->name = tc
290
291 • $shell->is_tc = 1
292
293 • $shell->is_c = 1
294
295 • $shell->is_unix = 1
296
297 • $shell->default_location = /bin/tcsh
298
299 All other instance methods will return false
300
301 z_shell
302 my $shell = Shell::Guess->z_shell
303
304 Returns an instance of Shell::Guess for zsh.
305
306 The following instance methods will return:
307
308 • $shell->name = z
309
310 • $shell->is_z = 1
311
312 • $shell->is_bourne = 1
313
314 • $shell->is_unix = 1
315
316 • $shell->default_location = /bin/zsh
317
318 All other instance methods will return false
319
321 The normal way to call these is by calling them on the result of either
322 running_shell or login_shell, but they can also be called as class
323 methods, in which case the currently running shell will be used, so
324
325 Shell::Guess->is_bourne
326
327 is the same as
328
329 Shell::Guess->running_shell->is_bourne
330
331 is_bash
332 my $bool = $shell->is_bash;
333
334 Returns true if the shell is bash.
335
336 is_bourne
337 my $bool = $shell->is_bourne;
338
339 Returns true if the shell is the bourne shell, or a shell which
340 supports bourne syntax (e.g. bash or korn).
341
342 is_c
343 my $bool = $shell->is_c;
344
345 Returns true if the shell is csh, or a shell which supports csh syntax
346 (e.g. tcsh).
347
348 is_cmd
349 my $bool = $shell->is_cmd;
350
351 Returns true if the shell is the Windows command.com shell.
352
353 is_command
354 my $bool = $shell->is_command;
355
356 Returns true if the shell is the Windows cmd.com shell.
357
358 is_dcl
359 my $bool = $shell->is_dcl;
360
361 Returns true if the shell is the OpenVMS dcl shell.
362
363 is_fish
364 my $bool = $shell->is_fish;
365
366 Returns true if the shell is Fish shell.
367
368 is_korn
369 my $bool = $shell->is_korn;
370
371 Returns true if the shell is the korn shell.
372
373 is_power
374 my $bool = $shell->is_power;
375
376 Returns true if the shell is Windows PowerShell.
377
378 is_tc
379 my $bool = $shell->is_tc;
380
381 Returns true if the shell is tcsh.
382
383 is_unix
384 my $bool = $shell->is_unix;
385
386 Returns true if the shell is traditionally a UNIX shell (e.g. bourne,
387 bash, korn)
388
389 is_vms
390 my $bool = $shell->is_vms;
391
392 Returns true if the shell is traditionally an OpenVMS shell (e.g. dcl)
393
394 is_win32
395 my $bool = $shell->is_win32;
396
397 Returns true if the shell is traditionally a Windows shell
398 (command.com, cmd.exe, powershell.exe, pwsh)
399
400 is_z
401 my $bool = $shell->is_z;
402
403 Returns true if the shell is zsh
404
405 name
406 my $name = $shell->name;
407
408 Returns the name of the shell.
409
410 default_location
411 my $location = $shell->default_location;
412
413 The usual location for this shell, for example /bin/sh for bourne shell
414 and /bin/csh for c shell. May not be defined for all shells.
415
417 Shell::Guess shouldn't ever die or crash, instead it will attempt to
418 make a guess or use a fallback about either the login or running shell
419 even on unsupported operating systems. The fallback is the most common
420 shell on the particular platform that you are using, so on UNIXy
421 platforms the fallback is bourne, and on OpenVMS the fallback is dcl.
422
423 These are the operating systems that have been tested in development
424 and are most likely to guess reliably.
425
426 • Linux
427
428 • Cygwin
429
430 • FreeBSD
431
432 • Mac OS X
433
434 • Windows (Strawberry Perl)
435
436 • Solaris (x86)
437
438 • MS-DOS (djgpp)
439
440 • OpenVMS
441
442 Always detected as dcl (a more nuanced view of OpenVMS is probably
443 possible, patches welcome).
444
445 UNIXy platforms without a proc filesystem will use Unix::Process if
446 installed, which will execute ps to determine the running shell.
447
448 It is pretty easy to fool the ->running_shell method by using fork, or
449 if your Perl script is not otherwise being directly executed by the
450 shell.
451
452 Patches are welcome to make other platforms work more reliably.
453
455 Author: Graham Ollis <plicease@cpan.org>
456
457 Contributors:
458
459 Buddy Burden (BAREFOOT)
460
461 Julien Fiegehenn (SIMBABQUE)
462
464 This software is copyright (c) 2012 by Graham Ollis.
465
466 This is free software; you can redistribute it and/or modify it under
467 the same terms as the Perl 5 programming language system itself.
468
469
470
471perl v5.38.0 2023-07-21 Shell::Guess(3)