Socket
Socket
Sign inDemoInstall

shx

Package Overview
Dependencies
2
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

shx

Portable Shell Commands for Node


Version published
Maintainers
2
Install size
649 kB
Created

Package description

What is shx?

The shx npm package provides a way to use shell commands in a cross-platform manner. It allows you to run common shell commands like `cp`, `rm`, `mv`, `echo`, and more, directly from your Node.js scripts or npm scripts, ensuring compatibility across different operating systems.

What are shx's main functionalities?

Copy files

This command copies a file from `source.txt` to `destination.txt`. It works across different operating systems without needing to worry about platform-specific syntax.

shx cp source.txt destination.txt

Remove files or directories

This command removes a file or directory at the specified path. The `-rf` flags ensure that the removal is recursive and forces the deletion without prompting.

shx rm -rf path/to/directory

Move or rename files

This command moves or renames a file from `oldname.txt` to `newname.txt`. It provides a simple way to handle file renaming or moving operations.

shx mv oldname.txt newname.txt

Echo text

This command prints the text 'Hello, World!' to the console. It can be used to output messages or variables in scripts.

shx echo 'Hello, World!'

Create directories

This command creates a new directory at the specified path. The `-p` flag ensures that any necessary parent directories are also created.

shx mkdir -p path/to/new/directory

Other packages similar to shx

Readme

Source

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 commandRecommend workaround
shx cdJust use plain old cd (it's the same on windows too)
shx pushdJust 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 popdJust use plain old popd
shx dirsNo workaround
shx setSee below
shx exitJust use plain old exit
shx execInstead of shx exec cmd, just use plain old cmd
shx ShellStringNo 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 flagshell.config settingshx commandEffect
-econfig.fatal = trueNot supportedExit upon first error
-vconfig.verbose = trueshx --verbose cd fooLog the command as it's run
-fconfig.noglob = trueshx --noglob cat '*.txt'Don't expand wildcards
N/Aconfig.silent = trueshx --silent cd noexistDon't show error output

Team

Nate FischerAri PoradLevi Thomason
Nate FischerAri PoradLevi Thomason

Keywords

FAQs

Last updated on 10 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc