Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

shx

shelljs2.3mMIT0.3.4

Portable Shell Commands for Node

shelljs, shell, unix, bash, sh, exec, cli, zsh

readme

Shx

Build Status Codecov npm version npm downloads

shx is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scripts.

shx is proudly tested on every node release since v6!

Difference Between ShellJS and shx

  • ShellJS: Good for writing long scripts, all in JS, running via NodeJS (e.g. node myScript.js).
  • shx: Good for writing one-off commands in npm package scripts (e.g. "clean": "shx rm -rf out/").

Install

npm install shx --save-dev

This will allow using shx in your package.json scripts.

Usage

Command Line

If you'd like to use shx on the command line, install it globally with the -g flag. The following code can be run either a Unix or Windows command line:

$ shx pwd                       # ShellJS commands are supported automatically
/home/username/path/to/dir

$ shx ls                        # files are outputted one per line
file.txt
file2.txt

$ shx rm *.txt                  # a cross-platform way to delete files!

$ shx ls

$ shx echo "Hi there!"
Hi there!

$ shx touch helloworld.txt

$ shx cp helloworld.txt foobar.txt

$ shx mkdir sub

$ shx ls
foobar.txt
helloworld.txt
sub

$ shx rm -r sub                 # options work as well

$ shx --silent ls fakeFileName  # silence error output

All commands internally call the ShellJS corresponding function, guaranteeing cross-platform compatibility.

package.json

ShellJS is good for writing long scripts. If you want to write bash-like, platform-independent scripts, we recommend you go with that.

However, shx is ideal for one-liners inside package.json:

{
  "scripts": {
    "clean": "shx rm -rf build dist && shx echo Done"
  }
}

Tip: because Windows treats single quotes (ex. 'some string') differently than double quotes, we recommend wrapping your arguments in double quotes for cross platform compatibility (ex. "some string").

Command reference

Shx exposes most ShellJS commands. If a command is not listed here, assume it's supported!

sed

Shx provides unix-like syntax on top of shell.sed(). So ShellJS code like:

shell.sed('-i', /original string/g, 'replacement', 'filename.txt');

would turn into the following Shx command:

shx sed -i "s/original string/replacement/g" filename.txt

Note: like unix sed, shx sed treats / as a special character, and this must be escaped (as \/ in the shell, or \\/ in package.json) if you intend to use this character in either the regex or replacement string. Do not escape / characters in the file path.

Unsupported Commands

As mentioned above, most ShellJS commands are supported in ShellJS. Due to the differences in execution environments between ShellJS and shx (JS vs CLI) the following commands are not supported:

Unsupported command Recommend workaround
shx cd Just use plain old cd (it's the same on windows too)
shx pushd Just use plain old pushd. Use forward slashes and double-quote the path. (e.g. pushd "../docs". This would fail on Windows without the quotes)
shx popd Just use plain old popd
shx dirs No workaround
shx set See below
shx exit Just use plain old exit
shx exec Instead of shx exec cmd, just use plain old cmd
shx ShellString No workaround (but why would you want this?)

Shx options

Shx allows you to modify its behavior by passing arguments. Here's a list of supported options:

set flag shell.config setting shx command Effect
-e config.fatal = true Not supported Exit upon first error
-v config.verbose = true shx --verbose cd foo Log the command as it's run
-f config.noglob = true shx --noglob cat '*.txt' Don't expand wildcards
N/A config.silent = true shx --silent cd noexist Don't show error output

Team

Nate Fischer Ari Porad Levi Thomason
Nate Fischer Ari Porad Levi Thomason

changelog

Change Log

Unreleased

Full Changelog

Closed issues:

  • New release with ShellJS v0.8.4 #185

Merged pull requests:

v0.3.3 (2020-10-26)

Full Changelog

Closed issues:

  • Npm package should always have a main module #180
  • shx stop working after a new library is added within package.json file #176
  • suggestion - expose commands directly (ls instead of shx ls) #174
  • Can I replace bash prompt with shx ? If so, how ? #171
  • sed can't replace files in a different directory #165
  • Issues running NPM script on Windows 10 #164
  • rm -rf random fails on windows #160
  • sed: option not recognized: n #147
  • Remove es6-object-assign dependency #146

Merged pull requests:

v0.3.2 (2018-07-11)

Full Changelog

Closed issues:

  • Mocha and babel compiler warnings #144
  • Add "true" and "false" commands #105
  • --help should list options #101

Merged pull requests:

v0.3.1 (2018-06-26)

Full Changelog

Closed issues:

  • curl / wget #141
  • Regex support for forward slashes in sed #136
  • Appveyor config broken for latest npm #132
  • shx echo -n aaa shows -n aaa #129
  • feature request: --version #111

Merged pull requests:

v0.3.0 (2018-06-08)

Full Changelog

Closed issues:

  • Ship shx with shelljs@v0.8 #125
  • Support executables #124
  • Discuss: popd/pushd workarounds #121
  • Feature request: date #120
  • Fails trying to utilise the new script-shell npm config #117
  • Refactor: consider using process.exitCode #116
  • Add node v8 to CI #114
  • can we use this without the shx prefix? #113
  • Command substitution feature #112

Merged pull requests:

v0.2.2 (2017-01-08)

Full Changelog

Closed issues:

  • sed does not accept a blank replacement string #109
  • chore: remove lgtm.co #102
  • chore: consider allowing iojs failures on travis #99
  • chore: switch appveyor to my account #97

Merged pull requests:

v0.2.1 (2016-11-19)

Full Changelog

Closed issues:

  • "shx cp" does not finish on Unix-Systems #95

Merged pull requests:

v0.2.0 (2016-11-08)

Full Changelog

Closed issues:

  • cp -r fails after 72 directories (Many files) #94
  • chore: add node v7 to CI #90
  • Unable to use in postinstall script on Windows #88
  • fs.existsSync is un-deprecated #87
  • piping from cat seems broken #85
  • List usb devices #84
  • Feature request: generic OR #83
  • Add -u flag support for cp #82
  • Commands should accept stdin #80
  • Add plugin support #73

Merged pull requests:

v0.1.4 (2016-08-18)

Full Changelog

Merged pull requests:

v0.1.3 (2016-08-10)

Full Changelog

Closed issues:

  • Using sed #74
  • cp -n should not produce errors if file exists #67
  • Generic NUL output redirect #61
  • Consider using publish-please #60
  • Explain unsupported commands and give workarounds #59
  • shx with no args: help message? #48
  • Setup changelog #18

Merged pull requests:

v0.1.2 (2016-05-11)

Full Changelog

Closed issues:

  • Code coverage badge seems to show for latest (passing) PR, not for master branch #51

Merged pull requests:

v0.1.1 (2016-05-03)

Full Changelog

Closed issues:

  • Broken release: missing runtime dependencies #43

Merged pull requests:

  • fix(deps): add babel-polyfill as a runtime dep #44 (nfischer)

v0.1.0 (2016-05-03)

Closed issues:

  • Linter checks the code coverage directory #36
  • Codecov.io repo settings #35
  • No rimraf command #27
  • Unable to install with npm #24
  • Switch to Mocha #22
  • Setup coverage #19
  • Setup Travis #17
  • Unable to install as a dependency #16
  • Switch this to use shelljs as a git submodule (for now) #13
  • Consider shx REPL #12
  • cd can't work, so we should output a warning #6
  • echo outputs twice #5
  • Initial Discussion and TODOs for 1st release #1

Merged pull requests:

* This Change Log was automatically generated by github_changelog_generator