Skip to content

Latest commit

History

History
75 lines (55 loc) 路 2.96 KB

File metadata and controls

75 lines (55 loc) 路 2.96 KB

馃捇 Shell

Shell types

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:

  • sh the ancestor of most of those shells.
  • projects like modernish.

However this won't work on Windows which uses two other shells by default:

  • cmd.exe which comes by default.
  • Powershell which is more recent, featureful and complex.

cmd.exe

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 the child_process.spawn() option windowsVerbatimArguments which defaults to true when cmd.exe is used.

As a consequence it is recommended to:

  • keep shell commands to simple command arguments... calls
  • use execa() (without its shell option) to fire those.

Command execution

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.

Terminal colors

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.

Summary

Fire shell commands with execa.


Next (馃捇 File execution)
Previous (馃捇 Terminal)
Top