paint-brush
How to Use ShellJS to Combine Bash Shell Commands and Javascriptby@iannguyen
4,109 reads
4,109 reads

How to Use ShellJS to Combine Bash Shell Commands and Javascript

by Ian NguyenOctober 22nd, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

ShellJS is a Javascript library written by Nate Fisher and Brandon Freitag. The library provides some Bash shell commands that can be executed in Node.js. ShellJS's power lies in the ability to execute terminal commands quickly, right from Javascript files. Developers can use the library to write automation scripts entirely in Javascript, which can simplify developers' experience. You can use ShellJS on both Windows and Linux as it works across platforms.

Coin Mentioned

Mention Thumbnail
featured image - How to Use ShellJS to Combine Bash Shell Commands and Javascript
Ian Nguyen HackerNoon profile picture

If you are looking for a way to use Bash or Unix shell commands within Javascript to automate your workflow, then ShellJS is the perfect library for you. ShellJS's power lies in the ability to execute terminal commands quickly, right from Javascript files. Developers can use the library to write automation scripts entirely in Javascript, which can simplify developers' experience.

What Is Bash Shell?

A Bash shell is a tool that interprets the lines of text entered in the terminal and interacts with the operating system to output the desired actions.


For example, if I enter cd .. in the terminal, Bash will understand that I want to move the current working directory up in one level.


Bash shell is also a programming language and a shell interpreter at the same time, according to this Bash Reference Manual. Bash shell allows users to create and manipulate variables, funtions. Users can also use Bash to execute conditional flow like if...do, or looping iteration like while...do just like Javascript.


Linux users who are familiar with Bash shell can write performant scripting files that automate common workflow on the system. This saves them a lot of time and prevents them from doing repetitive actions.

What Is ShellJS?

ShellJS is a Javascript library written by Nate Fisher and Brandon Freitag. The library provides some Bash shell commands that can be executed in Node.js runtime. A developer familiar with Bash shell commands can now use them in a Javascript file. For example:


shell.cd('./subfolder')


This is similar to the cd command in Bash:

$ cd ./subfolder


You can use ShellJS on both Windows and Linux as it works across platforms.


The library is used frequently in popular libraries and tools, like Docusaurus, Firebug, ESLint, and many more.


How to Use ShellJS

First, import the ShellJS library into your Javascript file.

const shell = require('shelljs')


Then use any of the library's methods to fit your development needs.

shell.mkdir('new-folder')


Read the entire ShellJS's documentation at: https://github.com/shelljs/shelljs.

ShellJS Example Usage

In this example, we'll use the ShellJS library to modifying some files and folders.


Initialize a local repository and create an index.js file:

npm init
touch index.js


In your index.js files, import the ShellJS library:

const shell = require('shelljs')


We will use the ShellJS's methods cd(), mkdir() , touch() which are very similar to using cd , mkdir , and touch commands in Bash shell.


Create a function called createNewFiles() and input the following lines:

function createNewFiles() {
    //creates a new folder
    shell.mkdir('delish-dishes')
    //navigate to the new folder directory
    shell.cd('./delish-dishes')
    //creates two new files: ingredients.txt and recipe.txt
    shell.touch(['ingredients.txt', 'recipe.txt'])
}


Then, we will input some content in the newly created text file. Create a new function called writeToNewFiles() and input the following lines. The echo() method prints contents to the console, similar to Bash's echo command. The shell.to() method behaves similar to the Bash redirect operator / , which prints the string to the ingredients.txt file.


function writeToNewFiles() {
    //creates a variable that stores a string to be appended to the new file
    let fileContent ='Ingredients for fried rice: rice, shallots, scalions, soy sauce, egg, sausage'
    //writing the printed string to the ingredients.txt file
    shell.echo(fileContent).to('ingredients.txt')
}


We need to invoke the above functions to run the commands inside them.

createNewFiles()
writeToNewFiles()


Finally, we'll use Node.js to run the javascript file. In your terminal console, input the command:

node index.js


After running the file, you will see that the ingredients.txt will contain the newly appended text. A new subfolder named delish-dishes was also created.

Mastering ShellJS

So you have learned how to use basic Bash commands using ShellJS in the above example. You can also use any Javascript methods, operators, and control flow to write your own application with ShellJS on the side.


For the full reference of all ShellJS methods and APIs, reference their Github's README.md file: https://github.com/shelljs/shelljs