Unix usually comes with Bash but not always. Popular alternatives include Fish, Dash, tcsh, ksh and zsh.
Writing interoperable shell code can be somewhat achieved by using either:
However this won't work on Windows which uses two other shells by default:
cmd.exewhich comes by default.- Powershell which is more recent, featureful and complex.
cmd.exe is very different from Bash and has quite many limitations:
;cannot be used to separate statements. However&&can be used like in Bash.- CLI flags often use slashes (
/opt) instead of dashes (-opt). But Node.js binaries can still use-opt. - Globbing (e.g. wildcard
*) does not work. - Exit code are accessed with
%errorlevel%instead of$?. - Escaping is done differently with
double quotes and
^. This is partially solved with thechild_process.spawn()optionwindowsVerbatimArgumentswhich defaults totruewhencmd.exeis used.
As a consequence it is recommended to:
- keep shell commands to simple
command arguments...calls - use
execa()(without itsshelloption) to fire those.
When the option shell of
child_process.spawn()
is true, /bin/sh will be used on Unix and cmd.exe (or the environment
variable ComSpec) will be used on Windows. Since those shells behave
differently it is better to avoid that option.
How many colors a terminal supports (if any) depends on both the operating system and the terminal itself.
You can use
process.stdout.getColorsDepth(),
process.stdout.hasColors()
or supports-color to detect these.
However this is usually not necessary as colors library like
chalk automatically do this.
Fire shell commands with execa.