- Notifications
You must be signed in to change notification settings - Fork50
Portable Shell Commands for Node
License
shelljs/shx
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
shx is a wrapper aroundShellJS Unixcommands, providing an easy solution for simple Unix-like, cross-platformcommands in npm package scripts.
shx is proudly tested on every LTS node release sincev20!
- 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/").
npm install shx --save-dev
This will allow usingshx in yourpackage.json scripts.
If you'd like to useshx on the command line, install it globally with the-g flag.The following code can be runeither a Unix or Windows command line:
$ shxpwd# ShellJS commands are supported automatically/home/username/path/to/dir$ shx ls# files are outputted one per linefile.txtfile2.txt$ shx rm*.txt# a cross-platform way to delete files!$ shx ls$ shxecho"Hi there!"Hi there!$ shx touch helloworld.txt$ shx cp helloworld.txt foobar.txt$ shx mkdir sub$ shx lsfoobar.txthelloworld.txtsub$ shx rm -r sub# options work as well$ shx --silent ls fakeFileName# silence error output$ shx --negatetest -d dir# Negate status code output (e.g., failed commands will now have status 0)
All commands internally call the ShellJS corresponding function, guaranteeingcross-platform compatibility.
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 insidepackage.json:
{"scripts": {"clean":"shx rm -rf\"build/**/*.js\"\"build/output\" && shx echo\"Done cleaning\"" }}It's safe to use&& and|| operators in npm package scripts. These will beinterpreted by the operating system's shell (sh on Unix,cmd.exe onWindows). If you're using glob operators like* or**, then we recommend toput these in double quotes, which ensures thatshx will expand the globrather than the operating system shell.
Important
Windows treats single quotes (ex.'some string') differently than doublequotes.We recommendwrapping your arguments inescaped double quotes so that your code iscompatible cross platform (ex."clean": "shx echo \"some string\"").
Shx exposesmost ShellJScommands. If a command isnot listed here, assume it's supported!
Shx provides unix-like syntax on top ofshell.sed(). So ShellJS code like:
shell.sed('-i',/originalstring/g,'replacement','filename.txt');
would turn into the following Shx command:
shx sed -i"s/original string/replacement/g" filename.txtNote: like unixsed,shx sed treats/ as a special character, andthis must beescaped (as\/ in the shell, or\\/ inpackage.json) if you intend to use thischaracter in either the regex or replacement string. Donot escape/characters in the file path.
As mentioned above, most ShellJS commands are supported inshx. Due to thedifferences in execution environments between ShellJS andshx (JS vs CLI) thefollowing commands are not supported:
| Unsupported command | Recommend workaround |
|---|---|
shx cd | Just use plain oldcd (it's the same on windows too) |
shx pushd | Just use plain oldpushd. 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 oldpopd |
shx dirs | No workaround |
shx set | See below |
shx exit | Just use plain oldexit |
shx exec | Instead ofshx exec cmd, just use plain oldcmd |
shx ShellString | No workaround (but why would you want this?) |
Shx allows you to modify its behavior by passing arguments. Here's a list ofsupported 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. |
| N/A | N/A | shx --negate test -d dir | Runs the specified command but negates the exit status. Failed command = status 0, successful command = status 1. |
| N/A | N/A | shx --help | Show help text. |
| N/A | N/A | shx --version | Print the shx version. |
Did you know thatshx can also take advantage of all the greatShellJS plugins?SeePLUGINS.md for instructions.
| Nate Fischer | Ari Porad | Levi Thomason |
About
Portable Shell Commands for Node
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.