BATsh
view release on metacpan or search on metacpan
ECHO text^ next line is joined (line continuation)
SH MODE
Any line whose first token contains a lowercase letter is a SH line. SH
sections are executed by BATsh::SH, which implements:
VAR=value, export VAR=value, unset VAR
echo, printf
if/then/elif/else/fi
for VAR in list; do ... done
while condition; do ... done
until condition; do ... done
case $var in pat1|pat2) ... ;; *) ... ;; esac
(case: |-patterns, * ? [abc] [a-z] [!abc] globs, quoted/literal
patterns, and the bash ;& / ;;& fall-through terminators)
trap 'cmd' SIG... / trap - SIG / trap '' SIG / trap [-p]
(EXIT pseudo-signal plus a pure-Perl %SIG bridge for real signals)
test / [ ... ] (file, string, and integer comparisons)
cd, pwd, exit, true, false, :, read, shift [N], local VAR=value
$(( arithmetic )) -- +, -, *, /, %, and $1..$9 inside
$( command ) and `command` (command substitution, nested)
cmd1 | cmd2 [| cmd3 ...] (pipeline via temporary file)
cmd1 && cmd2, cmd1 || cmd2, cmd1 ; cmd2 (compound commands)
> >> < 2> 2>> 2>&1 1>&2 (I/O redirection)
cmd << DELIM ... DELIM (here-document; <<-DELIM, <<'DELIM' too)
cmd & (background execution; external commands)
name() { ... }, function name { ... } (function definitions)
$VAR, ${VAR}, $1..$9, $@, $*, $#, $?, $$, $0, $!
${VAR:-default}, ${VAR:=default}, ${VAR:+alt}
${VAR%pat}, ${VAR%%pat} -- shortest/longest suffix removal
${VAR#pat}, ${VAR##pat} -- shortest/longest prefix removal
${VAR/pat/rep}, ${VAR//pat/rep} -- first/all substitution
${VAR^^}, ${VAR^}, ${VAR,,}, ${VAR,} -- case conversion
${VAR:N:L}, ${VAR:N} -- substring
${#VAR} -- string length
arr=(a b c), arr+=(d e), arr[i]=v -- indexed arrays
declare -a arr, declare -A map, typeset ... -- array declaration
map=([k]=v ...), map[k]=v -- associative arrays
${arr[i]}, ${map[key]}, $arr (== ${arr[0]}) -- element access
${arr[@]}, ${arr[*]}, ${#arr[@]}, ${#arr[i]}, ${!arr[@]}
unset arr, unset arr[i]
source / . file
INSTALLATION
To install from CPAN:
cpan BATsh
To install from a distribution tarball:
perl Makefile.PL
make
make test
make install
No non-core dependencies are required.
REQUIREMENTS
Perl 5.005_03 or later. Core modules only. No external shell required.
BUGS AND LIMITATIONS
Commands that are not built in -- "FINDSTR", "SORT", "MORE", "CHOICE",
"TIMEOUT", "XCOPY", "ROBOCOPY" and the like in CMD mode, and any
non-builtin program in SH mode -- are NOT reimplemented in Perl. They
are invoked as external programs (via Perl's "system"), so they work
only where the host operating system provides the corresponding
executable (e.g. FINDSTR.EXE on Windows). Only the built-in command set
is guaranteed to run identically on every platform.
The built-in CMD interpreter does not implement:
Variable substring "%VAR:~n,m%" / "%VAR:~n%" / "%VAR:~-n%" and
substitution "%VAR:str1=str2%" / "%VAR:*str1=str2%" are supported as of
version 0.05. Dynamic pseudo-variables "%DATE%" (YYYY-MM-DD), "%TIME%"
(HH:MM:SS.cc), "%CD%", "%RANDOM%" (0-32767), "%ERRORLEVEL%", and
"%CMDCMDLINE%" are also supported as of version 0.05.
* "FOR /F" with "usebackq" backtick-quoted commands on Windows (the
"cmd /c" subprocess path is untested on Windows).
The built-in SH interpreter does not support:
* Filename (pathname) globbing such as "echo *.txt" or "for f in
*.pl". The "*", "?" and "[abc]" metacharacters are honoured in
"case" patterns and in "${VAR%pat}" / "${VAR#pat}" expansion only,
not for expanding filenames on the command line.
* Tilde expansion "~/path" and "~user" (only "cd" with no argument
uses "$HOME").
* Brace expansion "{a,b}" and "{1..5}".
* Here-strings ("<<< word") and process substitution ("<(cmd)",
">(cmd)").
* The shell options "set -e", "set -u", "set -x", and the builtins
"getopts", "select", "alias", "eval" and "exec".
Common to both modes: a parenthesised group "( ... )" does not run in a
separate sub-shell; it shares the one variable store, so there is no
variable-scope isolation.
Here-documents ("<<", "<<-", "<<'DELIM'") ARE supported on STDIN, with
these limitations:
* Recognised only in SH mode; "<<" has no meaning in CMD mode.
* One here-document per command line only (no "cmd <<A <<B").
* Here-strings ("<<< word") are not supported.
* Combining a here-document with a pipeline or compound operator on
the same line is best-effort only; use a separate command.
* The closing delimiter must be on a line by itself and match
exactly (after tab stripping for "<<-").
Background execution (a trailing "&") IS supported in SH mode, with
these limitations:
* Applies to external commands only. A trailing "&" on a built-in, a
( run in 0.515 second using v1.01-cache-2.11-cpan-0b5f733616e )