Write Bash scripts with Zx: How to use

By XiaoXin
A Bit Randomly

To convert CSV to JSON in Node.js, you can use the csvtojson package from NPM. This package provides a simple and flexible API for converting CSV data to JavaScript objects. Here is an example of how to use the csvtojson p... Read How to Convert CSV to JSON in NodeJS

Main Contents

Write Bash scripts with Zx: How to use

First, let's create a new project:

mkdir zx-shell-scripts
cd zx-shell-scripts
npm init --yes

Then install the zx library:

npm install --save-dev zx

Note: zx's documentation recommends installing the library globally with npm. By installing it as a local dependency of our project, we can ensure that zx is always installed and control the version used by the shell script.

Top-level await

In order to use top-level Node.js await, i.e. awaitoutside of asyncfunctions, we need to write code in ES module mode, which supports top-level await.

We can indicate that all modules in the project are ES modules by package.jsonadding in "type": "module"Or we can set the file extension of individual scripts to .mjs. In the examples in this article, we will use the .mjsfile extension.

Run the command and capture the output

Create a new script, name it hello-world.mjs. We'll add a shebang line that tells the operating system's (OS) kernel to noderun the script as a program:

#! /usr/bin/env node

Then, we add some code to use zx to run commands.

In the code below, we run the command executor lslsThe program will list the files in the current working directory (the directory where the script is located). We'll capture standard output from the command's process, store it in a variable, and print it to the terminal:

// hello-world.mjs
import { $ } from "zx";
const output = (await $`ls`).stdout;

Note: The zx docs suggest putting /usr/bin/env zxput in the shebang line of our script, but we use /usr/bin/env nodeinstead. This is because we have installed zx as a local dependency of the project. We then explicitly import the functions and objects we want to use from the zx package. This helps clarify where the dependencies used in our script come from.

We chmodmake the script executable with:

chmod u+x hello-world.mjs

Run the project:


You can see the following output:

$ ls

You will notice:

  • The command we ran ( ls) is included in the output.
  • The output of the command is displayed twice.
  • There is an extra new line at the end of the output.

zx verboseruns in mode by default. $It will output the command you pass to the function, as well as the standard output of that command. lsWe can change this behavior by adding the following line of code before running the command:

$.verbose = false;

Most command-line programs, such as ls, output a newline character at the end of their output to make the output more readable in the terminal. This is good for readability, but since we're going to store the output in a variable, we don't want this extra new line. String#trim()We can remove it with a JavaScript function:

- const output = (await $`ls`).stdout;
+ const output = (await $`ls`).stdout.trim();

Running the script again, the result looks much better:



Please Share This Article Thank You!

Parts of Tutorial write Bash Scripts with ZX Library
Write Bash scripts with Zx: Import TypeScript

If we want to write shell scripts using zx in TypeScript, there are a few minor differences that we need to account for. Note: The TypeScript compiler provides a large number of configuration options that allow us to tweak...

What does Google's ZX Library do?

In this post, we'll learn what Google's Zx library provides and how we can use it to write shell scripts in Node.js. We'll then learn how to use the power of Zx building a command line tool that helps us bootstrap configur...