3 # This script is essentially copied from /usr/share/lintian/checks/scripts,
5 # Copyright (C) 1998 Richard Braakman
6 # Copyright (C) 2002 Josip Rodin
8 # Copyright (C) 2003 Julian Gilbey
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <https://www.gnu.org/licenses/>.
25 use Getopt::Long qw(:config bundling permute no_getopt_compat);
26 use File::Temp qw/tempfile/;
30 (my $progname = $0) =~ s|.*/||;
33 Usage: $progname [-n] [-f] [-x] script ...
35 or: $progname --version
36 This script performs basic checks for the presence of bashisms
37 in /bin/sh scripts and the lack of bashisms in /bin/bash ones.
40 my $version = <<"EOF";
41 This is $progname, from the Debian devscripts package, version ###VERSION###
42 This code is copyright 2003 by Julian Gilbey <jdg\@debian.org>,
43 based on original code which is copyright 1998 by Richard Braakman
44 and copyright 2002 by Josip Rodin.
45 This program comes with ABSOLUTELY NO WARRANTY.
46 You are free to redistribute this code under the terms of the
47 GNU General Public License, version 2, or (at your option) any later version.
50 my ($opt_echo, $opt_force, $opt_extra, $opt_posix);
51 my ($opt_help, $opt_version);
54 # Detect if STDIN is a pipe
55 if (scalar(@ARGV) == 0 && (-p STDIN or -f STDIN)) {
60 ## handle command-line options
62 $opt_help = 1 if int(@ARGV) == 0;
65 "help|h" => \$opt_help,
66 "version|v" => \$opt_version,
67 "newline|n" => \$opt_echo,
68 "force|f" => \$opt_force,
69 "extra|x" => \$opt_extra,
70 "posix|p" => \$opt_posix,
73 "Usage: $progname [options] filelist\nRun $progname --help for more details\n";
75 if ($opt_help) { print $usage; exit 0; }
76 if ($opt_version) { print $version; exit 0; }
78 $opt_echo = 1 if $opt_posix;
84 my (%bashisms, %string_bashisms, %singlequote_bashisms);
87 = qr'(?:(?:^|[`&;(|{])\s*|(?:(?:if|elif|while)(?:\s+!)?|then|do|shell)\s+)';
90 my @bashisms_keys = sort keys %bashisms;
91 my @string_bashisms_keys = sort keys %string_bashisms;
92 my @singlequote_bashisms_keys = sort keys %singlequote_bashisms;
94 foreach my $filename (@ARGV) {
95 my $check_lines_count = -1;
97 my $display_filename = $filename;
99 if ($filename eq '-') {
102 = tempfile("chkbashisms_tmp.XXXX", TMPDIR => 1, UNLINK => 1);
103 while (my $line = <STDIN>) {
107 $display_filename = "(stdin)";
111 $check_lines_count = script_is_evil_and_wrong($filename);
114 if ($check_lines_count == 0 or $check_lines_count == 1) {
116 "script $display_filename does not appear to be a /bin/sh script; skipping\n";
120 if ($check_lines_count != -1) {
122 "script $display_filename appears to be a shell wrapper; only checking the first "
123 . "$check_lines_count lines\n";
126 unless (open C, '<', $filename) {
127 warn "cannot open script $display_filename for reading: $!\n";
135 my $cat_indented = 0;
136 my $quote_string = "";
137 my $last_continued = 0;
140 my $buffered_orig_line = "";
141 my $buffered_line = "";
145 next unless ($check_lines_count == -1 or $. <= $check_lines_count);
147 if ($. == 1) { # This should be an interpreter line
148 if (m,^\#!\s*(?:\S+/env\s+)?(\S+),) {
149 my $interpreter = $1;
151 if ($interpreter =~ m,(?:^|/)make$,) {
152 init_hashes if !$makefile++;
155 init_hashes if $makefile--;
160 if ($interpreter =~ m,(?:^|/)bash$,) {
162 } elsif ($interpreter !~ m,(?:^|/)(sh|dash|posh)$,) {
165 "script $display_filename does not appear to be a /bin/sh script; skipping\n";
171 "script $display_filename does not appear to have a \#! interpreter line;\nyou may get strange results\n";
178 # We want to remove end-of-line comments, so need to skip
179 # comments that appear inside balanced pairs
180 # of single or double quotes
182 # Remove comments in the "quoted" part of a line that starts
183 # in a quoted block? The problem is that we have no idea
184 # whether the program interpreting the block treats the
185 # quote character as part of the comment or as a quote
186 # terminator. We err on the side of caution and assume it
187 # will be treated as part of the comment.
188 # s/^(?:.*?[^\\])?$quote_string(.*)$/$1/ if $quote_string ne "";
192 && $quote_string eq ''
193 && $buffered_line eq ''
194 && $cat_string eq '') {
198 # Remove quoted strings so we can more easily ignore comments
200 s/(^|[^\\](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
201 s/(^|[^\\](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
203 # If inside a quoted string, remove everything before the quote
205 if ($quote_string eq "'");
207 if ($quote_string eq '"');
209 # If the remaining string contains what looks like a comment,
210 # eat it. In either case, swap the unmodified script line
211 # back in for processing.
212 if (m/(?:^|[^[\\])[\s\&;\(\)](\#.*$)/) {
214 s/\Q$1\E//; # eat comments
219 # Handle line continuation
220 if (!$makefile && $cat_string eq '' && m/\\$/) {
222 $buffered_line .= $_;
223 $buffered_orig_line .= $orig_line . "\n";
227 if ($buffered_line ne '') {
228 $_ = $buffered_line . $_;
229 $orig_line = $buffered_orig_line . $orig_line;
231 $buffered_orig_line = '';
235 $last_continued = $continued;
242 # Don't match lines that look like a rule if we're in a
243 # continuation line before the start of the rules
244 if (/^[\w%-]+:+\s.*?;?(.*)$/
245 and !($last_continued and !$found_rules)) {
251 if m%^\s*(override\s|export\s)?\s*SHELL\s*:?=\s*(/bin/)?bash\s*%;
253 # Remove "simple" target names
254 s/^[\w%.-]+(?:\s+[\w%.-]+)*::?//;
256 s/(?<!\$)\$\((\w+)\)/\${$1}/g;
258 s/^[\s\t]*[@-]{1,2}//;
263 && (m/^\Q$cat_string\E$/
264 || ($cat_indented && m/^\t*\Q$cat_string\E$/))
269 my $within_another_shell = 0;
270 if (m,(^|\s+)((/usr)?/bin/)?((b|d)?a|k|z|t?c)sh\s+-c\s*.+,) {
271 $within_another_shell = 1;
273 # if cat_string is set, we are in a HERE document and need not
275 if ($cat_string eq "" and !$within_another_shell) {
278 my $explanation = '';
281 # Remove "" / '' as they clearly aren't quoted strings
282 # and not considering them makes the matching easier
283 $line =~ s/(^|[^\\])(\'\')+/$1/g;
284 $line =~ s/(^|[^\\])(\"\")+/$1/g;
286 if ($quote_string ne "") {
287 my $otherquote = ($quote_string eq "\"" ? "\'" : "\"");
288 # Inside a quoted block
289 if ($line =~ /(?:^|^.*?[^\\])$quote_string(.*)$/) {
291 my $templine = $line;
293 # Remove quoted strings delimited with $otherquote
295 =~ s/(^|[^\\])$otherquote[^$quote_string]*?[^\\]$otherquote/$1/g;
296 # Remove quotes that are themselves quoted
299 =~ s/(^|[^\\])$otherquote.*?$quote_string.*?[^\\]$otherquote/$1/g;
302 =~ s/(^|[^\\])$quote_string\\$quote_string$quote_string/$1/g;
304 # After all that, were there still any quotes left?
305 my $count = () = $templine =~ /(^|[^\\])$quote_string/g;
308 $count = () = $rest =~ /(^|[^\\])$quote_string/g;
309 if ($count % 2 == 0) {
310 # Quoted block ends on this line
311 # Ignore everything before the closing quote
318 # Still inside the quoted block, skip this line
323 # Check even if we removed the end of a quoted block
324 # in the previous check, as a single line can end one
325 # block and begin another
326 if ($quote_string eq "") {
327 # Possible start of a quoted block
328 for my $quote ("\"", "\'") {
329 my $templine = $line;
330 my $otherquote = ($quote eq "\"" ? "\'" : "\"");
332 # Remove balanced quotes and their content
334 my ($length_single, $length_double) = (0, 0);
336 # Determine which one would match first:
338 =~ m/(^.+?(?:^|[^\\\"](?:\\\\)*)\')[^\']*\'/) {
339 $length_single = length($1);
342 =~ m/(^.*?(?:^|[^\\\'](?:\\\\)*)\")(?:\\.|[^\\\"])+\"/
344 $length_double = length($1);
347 # Now simplify accordingly (shorter is preferred):
350 && ( $length_single < $length_double
351 || $length_double == 0)
353 $templine =~ s/(^|[^\\\"](?:\\\\)*)\'[^\']*\'/$1/;
354 } elsif ($length_double != 0) {
356 =~ s/(^|[^\\\'](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1/;
362 # Don't flag quotes that are themselves quoted
364 $templine =~ s/$otherquote.*?$quote.*?$otherquote//g;
366 $templine =~ s/(^|[^\\])$quote\\$quote$quote/$1/g;
368 $templine =~ s/\\[\'\"]//g;
369 my $count = () = $templine =~ /(^|(?!\\))$quote/g;
371 # If there's an odd number of non-escaped
372 # quotes in the line it's almost certainly the
373 # start of a quoted block.
374 if ($count % 2 == 1) {
375 $quote_string = $quote;
376 $start_lines{'quote_string'} = $.;
377 $line =~ s/^(.*)$quote.*$/$1/;
383 # since this test is ugly, I have to do it by itself
384 # detect source (.) trying to pass args to the command it runs
385 # The first expression weeds out '. "foo bar"'
388 m/$LEADIN\.\s+(\"[^\"]+\"|\'[^\']+\'|\$\([^)]+\)+(?:\/[^\s;]+)?)\s*(\&|\||\d?>|<|;|\Z)/o
389 and m/$LEADIN(\.\s+[^\s;\`:]+\s+([^\s;]+))/o) {
390 if ($2 =~ /^(\&|\||\d?>|<)/) {
396 $explanation = "sourced script with arguments";
397 output_explanation($display_filename, $orig_line,
402 # Remove "quoted quotes". They're likely to be inside
403 # another pair of quotes; we're not interested in
404 # them for their own sake and removing them makes finding
405 # the limits of the outer pair far easier.
406 $line =~ s/(^|[^\\\'\"])\"\'\"/$1/g;
407 $line =~ s/(^|[^\\\'\"])\'\"\'/$1/g;
409 foreach my $re (@singlequote_bashisms_keys) {
410 my $expl = $singlequote_bashisms{$re};
411 if ($line =~ m/($re)/) {
414 $explanation = $expl;
415 output_explanation($display_filename, $orig_line,
420 my $re = '(?<![\$\\\])\$\'[^\']+\'';
421 if ($line =~ m/(.*)($re)/o) {
422 my $count = () = $1 =~ /(^|[^\\])\'/g;
423 if ($count % 2 == 0) {
424 output_explanation($display_filename, $orig_line,
425 q<$'...' should be "$(printf '...')">);
429 # $cat_line contains the version of the line we'll check
430 # for heredoc delimiters later. Initially, remove any
431 # spaces between << and the delimiter to make the following
432 # updates to $cat_line easier. However, don't remove the
433 # spaces if the delimiter starts with a -, as that changes
434 # how the delimiter is searched.
435 my $cat_line = $line;
436 $cat_line =~ s/(<\<-?)\s+(?!-)/$1/g;
438 # Ignore anything inside single quotes; it could be an
439 # argument to grep or the like.
440 $line =~ s/(^|[^\\\"](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
442 # As above, with the exception that we don't remove the string
443 # if the quote is immediately preceded by a < or a -, so we
444 # can match "foo <<-?'xyz'" as a heredoc later
445 # The check is a little more greedy than we'd like, but the
446 # heredoc test itself will weed out any false positives
447 $cat_line =~ s/(^|[^<\\\"-](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
449 $re = '(?<![\$\\\])\$\"[^\"]+\"';
450 if ($line =~ m/(.*)($re)/o) {
451 my $count = () = $1 =~ /(^|[^\\])\"/g;
452 if ($count % 2 == 0) {
453 output_explanation($display_filename, $orig_line,
454 q<$"foo" should be eval_gettext "foo">);
458 foreach my $re (@string_bashisms_keys) {
459 my $expl = $string_bashisms{$re};
460 if ($line =~ m/($re)/) {
463 $explanation = $expl;
464 output_explanation($display_filename, $orig_line,
469 # We've checked for all the things we still want to notice in
470 # double-quoted strings, so now remove those strings as well.
471 $line =~ s/(^|[^\\\'](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
472 $cat_line =~ s/(^|[^<\\\'-](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
473 foreach my $re (@bashisms_keys) {
474 my $expl = $bashisms{$re};
475 if ($line =~ m/($re)/) {
478 $explanation = $expl;
479 output_explanation($display_filename, $orig_line,
483 # This check requires the value to be compared, which could
484 # be done in the regex itself but requires "use re 'eval'".
485 # So it's better done in its own
486 if ($line =~ m/$LEADIN((?:exit|return)\s+(\d{3,}))/o && $2 > 255) {
487 $explanation = 'exit|return status code greater than 255';
488 output_explanation($display_filename, $orig_line,
492 # Only look for the beginning of a heredoc here, after we've
493 # stripped out quoted material, to avoid false positives.
495 =~ m/(?:^|[^<])\<\<(\-?)\s*(?:(?!<|'|")((?:[^\s;>|]+(?:(?<=\\)[\s;>|])?)+)|[\'\"](.*?)[\'\"])/
497 $cat_indented = ($1 && $1 eq '-') ? 1 : 0;
498 my $quoted = defined($3);
499 $cat_string = $quoted ? $3 : $2;
501 # Now strip backslashes. Keep the position of the
502 # last match in a variable, as s/// resets it back
503 # to undef, but we don't want that.
505 pos($cat_string) = $pos;
506 while ($cat_string =~ s/\G(.*?)\\/$1/) {
507 # position += length of match + the character
508 # that followed the backslash:
509 $pos += length($1) + 1;
510 pos($cat_string) = $pos;
513 $start_lines{'cat_string'} = $.;
519 "error: $display_filename: Unterminated heredoc found, EOF reached. Wanted: <$cat_string>, opened in line $start_lines{'cat_string'}\n"
520 if ($cat_string ne '');
522 "error: $display_filename: Unterminated quoted string found, EOF reached. Wanted: <$quote_string>, opened in line $start_lines{'quote_string'}\n"
523 if ($quote_string ne '');
524 warn "error: $display_filename: EOF reached while on line continuation.\n"
525 if ($buffered_line ne '');
529 if ($mode && !$issues) {
530 warn "could not find any possible bashisms in bash script $filename\n";
537 sub output_explanation {
538 my ($filename, $line, $explanation) = @_;
541 # When examining a bash script, just flag that there are indeed
545 warn "possible bashism in $filename line $. ($explanation):\n$line\n";
550 # Returns non-zero if the given file is not actually a shell script,
551 # just looks like one.
552 sub script_is_evil_and_wrong {
555 # lintian's version of this function aborts if the file
556 # can't be opened, but we simply return as the next
557 # test in the calling code handles reporting the error
559 open(IN, '<', $filename) or return $ret;
562 my $backgrounded = 0;
571 # the exec should either be "eval"ed or a new statement
572 (^\s*|\beval\s*[\'\"]|(;|&&|\b(then|else))\s*)
574 # eat anything between the exec and $0
577 # optionally quoted executable name (via $0)
580 # optional "end of options" indicator
583 # Match expressions of the form '${1+$@}', '${1:+"$@"',
584 # '"${1+$@', "$@", etc where the quotes (before the dollar
585 # sign(s)) are optional and the second (or only if the $1
586 # clause is omitted) parameter may be $@ or $*.
588 # Finally the whole subexpression may be omitted for scripts
589 # which do not pass on their parameters (i.e. after re-execing
590 # they take their parameters (and potentially data) from stdin
591 .?(\$\{1:?\+.?)?(\$(\@|\*))?~x
595 } elsif (/^\s*(\w+)=\$0;/) {
599 # Match scripts which use "foo $0 $@ &\nexec true\n"
606 .?(\$\{1:?\+.?)?(\$(\@|\*))?.?\s*\&~x
613 # the exec should either be "eval"ed or a new statement
614 (^\s*|\beval\s*[\'\"]|(;|&&|\b(then|else))\s*)
620 } elsif (m~\@DPATCH\@~) {
633 qr'(?:^|\s+)function [^<>\(\)\[\]\{\};|\s]+(\s|\(|\Z)' =>
634 q<'function' is useless>,
635 $LEADIN . qr'select\s+\w+' => q<'select' is not POSIX>,
636 qr'(test|-o|-a)\s*[^\s]+\s+==\s' => q<should be 'b = a'>,
637 qr'\[\s+[^\]]+\s+==\s' => q<should be 'b = a'>,
638 qr'\s\|\&' => q<pipelining is not POSIX>,
639 qr'[^\\\$]\{([^\s\\\}]*?,)+[^\\\}\s]*\}' => q<brace expansion>,
640 qr'\{\d+\.\.\d+(?:\.\.\d+)?\}' =>
641 q<brace expansion, {a..b[..c]}should be $(seq a [c] b)>,
642 qr'(?i)\{[a-z]\.\.[a-z](?:\.\.\d+)?\}' => q<brace expansion>,
643 qr'(?:^|\s+)\w+\[\d+\]=' => q<bash arrays, H[0]>,
645 . qr'read\s+(?:-[a-qs-zA-Z\d-]+)' =>
646 q<read with option other than -r>,
648 . qr'read\s*(?:-\w+\s*)*(?:\".*?\"|[\'].*?[\'])?\s*(?:;|$)' =>
649 q<read without variable>,
650 $LEADIN . qr'echo\s+(-n\s+)?-n?en?\s' => q<echo -e>,
651 $LEADIN . qr'exec\s+-[acl]' => q<exec -c/-l/-a name>,
652 $LEADIN . qr'let\s' => q<let ...>,
653 qr'(?<![\$\(])\(\(.*\)\)' => q<'((' should be '$(('>,
654 qr'(?:^|\s+)(\[|test)\s+-a' => q<test with unary -a (should be -e)>,
655 qr'\&>' => q<should be \>word 2\>&1>,
656 qr'(<\&|>\&)\s*((-|\d+)[^\s;|)}`&\\\\]|[^-\d\s]+(?<!\$)(?!\d))' =>
657 q<should be \>word 2\>&1>,
659 q<alternative test command ([[ foo ]] should be [ foo ])>,
660 qr'/dev/(tcp|udp)' => q</dev/(tcp|udp)>,
661 $LEADIN . qr'builtin\s' => q<builtin>,
662 $LEADIN . qr'caller\s' => q<caller>,
663 $LEADIN . qr'compgen\s' => q<compgen>,
664 $LEADIN . qr'complete\s' => q<complete>,
665 $LEADIN . qr'declare\s' => q<declare>,
666 $LEADIN . qr'dirs(\s|\Z)' => q<dirs>,
667 $LEADIN . qr'disown\s' => q<disown>,
668 $LEADIN . qr'enable\s' => q<enable>,
669 $LEADIN . qr'mapfile\s' => q<mapfile>,
670 $LEADIN . qr'readarray\s' => q<readarray>,
671 $LEADIN . qr'shopt(\s|\Z)' => q<shopt>,
672 $LEADIN . qr'suspend\s' => q<suspend>,
673 $LEADIN . qr'time\s' => q<time>,
674 $LEADIN . qr'type\s' => q<type>,
675 $LEADIN . qr'typeset\s' => q<typeset>,
676 $LEADIN . qr'ulimit(\s|\Z)' => q<ulimit>,
677 $LEADIN . qr'set\s+-[BHT]+' => q<set -[BHT]>,
678 $LEADIN . qr'alias\s+-p' => q<alias -p>,
679 $LEADIN . qr'unalias\s+-a' => q<unalias -a>,
680 $LEADIN . qr'local\s+-[a-zA-Z]+' => q<local -opt>,
681 # function '=' is special-cased due to bash arrays (think of "foo=()")
682 qr'(?:^|\s)\s*=\s*\(\s*\)\s*([\{|\(]|\Z)' =>
683 q<function names should only contain [a-z0-9_]>,
684 qr'(?:^|\s)(?<func>function\s)?\s*(?:[^<>\(\)\[\]\{\};|\s]*[^<>\(\)\[\]\{\};|\s\w][^<>\(\)\[\]\{\};|\s]*)(?(<func>)(?=)|(?<!=))\s*(?(<func>)(?:\(\s*\))?|\(\s*\))\s*([\{|\(]|\Z)'
685 => q<function names should only contain [a-z0-9_]>,
686 $LEADIN . qr'(push|pop)d(\s|\Z)' => q<(push|pop)d>,
687 $LEADIN . qr'export\s+-[^p]' => q<export only takes -p as an option>,
688 qr'(?:^|\s+)[<>]\(.*?\)' => q<\<() process substitution>,
689 $LEADIN . qr'readonly\s+-[af]' => q<readonly -[af]>,
690 $LEADIN . qr'(sh|\$\{?SHELL\}?) -[rD]' => q<sh -[rD]>,
691 $LEADIN . qr'(sh|\$\{?SHELL\}?) --\w+' => q<sh --long-option>,
692 $LEADIN . qr'(sh|\$\{?SHELL\}?) [-+]O' => q<sh [-+]O>,
693 qr'\[\^[^]]+\]' => q<[^] should be [!]>,
696 q<'printf -v var ...' should be var='$(printf ...)'>,
697 $LEADIN . qr'coproc\s' => q<coproc>,
698 qr';;?&' => q<;;& and ;& special case operators>,
699 $LEADIN . qr'jobs\s' => q<jobs>,
700 # $LEADIN . qr'jobs\s+-[^lp]\s' => q<'jobs' with option other than -l or -p>,
702 . qr'command\s+-[^p]\s' => q<'command' with option other than -p>,
705 q<setvar 'foo' 'bar' should be eval 'foo="'"$bar"'"'>,
707 . qr'trap\s+["\']?.*["\']?\s+.*(?:ERR|DEBUG|RETURN)' =>
708 q<trap with ERR|DEBUG|RETURN>,
710 . qr'(?:exit|return)\s+-\d' =>
711 q<exit|return with negative status code>,
713 . qr'(?:exit|return)\s+--' =>
714 q<'exit --' should be 'exit' (idem for return)>,
716 . qr'sleep\s+(?:-|\d+(?:[.a-z]|\s+\d))' =>
717 q<sleep only takes one integer>,
718 $LEADIN . qr'hash(\s|\Z)' => q<hash>,
719 qr'(?:[:=\s])~(?:[+-]|[+-]?\d+)(?:[/\s]|\Z)' =>
720 q<non-standard tilde expansion>,
724 qr'\$\[[^][]+\]' => q<'$[' should be '$(('>,
725 qr'\$\{(?:\w+|@|\*)\:(?:\d+|\$\{?\w+\}?)+(?::(?:\d+|\$\{?\w+\}?)+)?\}'
727 qr'\$\{!\w+[\@*]\}' => q<${!prefix[*|@]>,
728 qr'\$\{!\w+\}' => q<${!name}>,
729 qr'\$\{(?:\w+|@|\*)([,^]{1,2}.*?)\}' =>
730 q<${parm,[,][pat]} or ${parm^[^][pat]}>,
731 qr'\$\{[@*]([#%]{1,2}.*?)\}' => q<${[@|*]#[#]pat} or ${[@|*]%[%]pat}>,
732 qr'\$\{#[@*]\}' => q<${#@} or ${#*}>,
733 qr'\$\{(?:\w+|@|\*)(/.+?){1,2}\}' => q<${parm/?/pat[/str]}>,
734 qr'\$\{\#?\w+\[.+\](?:[/,:#%^].+?)?\}' =>
735 q<bash arrays, ${name[0|*|@]}>,
736 qr'\$\{?RANDOM\}?\b' => q<$RANDOM>,
737 qr'\$\{?(OS|MACH)TYPE\}?\b' => q<$(OS|MACH)TYPE>,
738 qr'\$\{?HOST(TYPE|NAME)\}?\b' => q<$HOST(TYPE|NAME)>,
739 qr'\$\{?DIRSTACK\}?\b' => q<$DIRSTACK>,
740 qr'\$\{?EUID\}?\b' => q<$EUID should be "$(id -u)">,
741 qr'\$\{?UID\}?\b' => q<$UID should be "$(id -ru)">,
742 qr'\$\{?SECONDS\}?\b' => q<$SECONDS>,
743 qr'\$\{?BASH_[A-Z]+\}?\b' => q<$BASH_SOMETHING>,
744 qr'\$\{?SHELLOPTS\}?\b' => q<$SHELLOPTS>,
745 qr'\$\{?PIPESTATUS\}?\b' => q<$PIPESTATUS>,
746 qr'\$\{?SHLVL\}?\b' => q<$SHLVL>,
747 qr'\$\{?FUNCNAME\}?\b' => q<$FUNCNAME>,
748 qr'\$\{?TMOUT\}?\b' => q<$TMOUT>,
749 qr'(?:^|\s+)TMOUT=' => q<TMOUT=>,
750 qr'\$\{?TIMEFORMAT\}?\b' => q<$TIMEFORMAT>,
751 qr'(?:^|\s+)TIMEFORMAT=' => q<TIMEFORMAT=>,
752 qr'(?<![$\\])\$\{?_\}?\b' => q<$_>,
753 qr'(?:^|\s+)GLOBIGNORE=' => q<GLOBIGNORE=>,
754 qr'<<<' => q<\<\<\< here string>,
756 . qr'echo\s+(?:-[^e\s]+\s+)?\"[^\"]*(\\[abcEfnrtv0])+.*?[\"]' =>
757 q<unsafe echo with backslash>,
758 qr'\$\(\([\s\w$*/+-]*\w\+\+.*?\)\)' =>
759 q<'$((n++))' should be '$n; $((n=n+1))'>,
760 qr'\$\(\([\s\w$*/+-]*\+\+\w.*?\)\)' =>
761 q<'$((++n))' should be '$((n=n+1))'>,
762 qr'\$\(\([\s\w$*/+-]*\w\-\-.*?\)\)' =>
763 q<'$((n--))' should be '$n; $((n=n-1))'>,
764 qr'\$\(\([\s\w$*/+-]*\-\-\w.*?\)\)' =>
765 q<'$((--n))' should be '$((n=n-1))'>,
766 qr'\$\(\([\s\w$*/+-]*\*\*.*?\)\)' => q<exponentiation is not POSIX>,
767 $LEADIN . qr'printf\s["\'][^"\']*?%q.+?["\']' => q<printf %q>,
770 %singlequote_bashisms = (
772 . qr'echo\s+(?:-[^e\s]+\s+)?\'[^\']*(\\[abcEfnrtv0])+.*?[\']' =>
773 q<unsafe echo with backslash>,
775 . qr'source\s+[\"\']?(?:\.\/|\/|\$|[\w~.-])\S*' =>
776 q<should be '.', not 'source'>,
780 $bashisms{ $LEADIN . qr'echo\s+-[A-Za-z]*n' } = q<echo -n>;
783 $bashisms{ $LEADIN . qr'local\s+\w+(\s+\W|\s*[;&|)]|$)' }
785 $bashisms{ $LEADIN . qr'local\s+\w+=' } = q<local foo=bar>;
786 $bashisms{ $LEADIN . qr'local\s+\w+\s+\w+' } = q<local x y>;
787 $bashisms{ $LEADIN . qr'((?:test|\[)\s+.+\s-[ao])\s' } = q<test -a/-o>;
788 $bashisms{ $LEADIN . qr'kill\s+-[^sl]\w*' } = q<kill -[0-9] or -[A-Z]>;
789 $bashisms{ $LEADIN . qr'trap\s+["\']?.*["\']?\s+.*[1-9]' }
790 = q<trap with signal numbers>;
794 $string_bashisms{qr'(\$\(|\`)\s*\<\s*([^\s\)]{2,}|[^DF])\s*(\)|\`)'}
795 = q<'$(\< foo)' should be '$(cat foo)'>;
797 $bashisms{ $LEADIN . qr'\w+\+=' } = q<should be VAR="${VAR}foo">;
798 $string_bashisms{qr'(\$\(|\`)\s*\<\s*\S+\s*(\)|\`)'}
799 = q<'$(\< foo)' should be '$(cat foo)'>;
803 $string_bashisms{qr'\$\{?BASH\}?\b'} = q<$BASH>;
804 $string_bashisms{qr'(?:^|\s+)RANDOM='} = q<RANDOM=>;
805 $string_bashisms{qr'(?:^|\s+)(OS|MACH)TYPE='} = q<(OS|MACH)TYPE=>;
806 $string_bashisms{qr'(?:^|\s+)HOST(TYPE|NAME)='} = q<HOST(TYPE|NAME)=>;
807 $string_bashisms{qr'(?:^|\s+)DIRSTACK='} = q<DIRSTACK=>;
808 $string_bashisms{qr'(?:^|\s+)EUID='} = q<EUID=>;
809 $string_bashisms{qr'(?:^|\s+)UID='} = q<UID=>;
810 $string_bashisms{qr'(?:^|\s+)BASH(_[A-Z]+)?='} = q<BASH(_SOMETHING)=>;
811 $string_bashisms{qr'(?:^|\s+)SHELLOPTS='} = q<SHELLOPTS=>;
812 $string_bashisms{qr'\$\{?POSIXLY_CORRECT\}?\b'} = q<$POSIXLY_CORRECT>;