Linux echo command and scripting with nodejs

So this will be a quick post on using the Linux echo command and node.js when it comes to creating shell scripts with javaScript rather than the usual Bourne Shell. The echo command just simply prints something to the standard output, in some cases now and then I find myself using it. For example just simply piping in some kind of simple test input to a CLI tools standard input I am putting together that will accept such input from something else when it comes to a read use case scenario.

1 - Some basics of the Linux echo command by itself

So before getting into some javaScript it might be best to just start out playing around with some commands in the terminal with Linux echo by itself. I would say that the echo command is one of those basic commands in Linux that everyone that uses Linux, or a posix environment in general should be aware of. The command comes up a lot, so it pays to be familiar with many of the options ans and basic use case examples.

1.1 - basic Linux Echo

So if I type echo in the command line and then give a string the echo command will just echo that to the standard output of the console.

1
2
$ echo "hello world"
hello world

That is it that is the basic idea of what echo is for.

1.2 - Sometimes I might need to escape things

In some situations I might need to use the escape option so that things will render the way that they should in the console, or in any destination in which the result will ultimately end up. For example say I want to put some new lines in a string with the backslash n syntax.

1
2
3
$ echo -e 'foo\nbar\n'
foo
bar

So this is the only option I often find myself using now and then so I thought I would have a brief section here on this.

2 - The Linux Echo command and other ways to log to the standard output with nodejs

So to just use the Linux echo command by itself in the bash shell I type echo and then the string value that is to be piped to the standard output. There are a few options but for the most part that is all there is to the echo command in Linux.

1
2
$ echo "hello world"
hello world

So when working out a nodejs script that needs to split something out to the standard output, I could use the spawn child process to use the echo command. However there are some other ways to do so that are more piratical when working in a javaScript environment. Namely there is the console log method that will also echo something to the standard output and append a line beak just line that of the Linux echo command. In addition there is also the stdout property of the process global that is an instance of a writable stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// sure I could use echo in nodejs via spawn
let spawn = require('child_process').spawn;
// to do so I will want to use the stdio
// option of spawn to set 'inherit' rather
// than the default 'pipe' value for the
// standard output
let echo = spawn('echo', ['Hello world, spawn style using Linux echo.'], {
stdio: ['pipe', 'inherit', 'pipe']
});
// but there is also the sdout property of the process global
let os = require('os');
process.stdout.write('Hello World, process stdout style.' + os.EOL);
// or just simply console.log
console.log('Hello world, console log style');

3 - Using Linux Echo to pipe in some data to the standard input of a nodejs script

One of the typical use case examples when making a CLI tool, or Shell Script with nodejs is to use echo to pipe some kind of test input to the standard input of a script. In my nodejs script I can just use the stdin property of the process global to attach an event handler that will do something on a per chunk bases with that input.

Fo a quick example here is a script that just converts the data that is piped in to hex.

1
2
3
4
5
6
// do something with data that is being
// piped in from the standard input from a
// command like Linux echo
process.stdin.on('data', (data) => {
console.log(data.toString('hex'));
});
1
2
// $ echo "hello" | node echo_pipe
// 68656c6c6f0a

With many of my real projects so far what is actually being piped in might be the full body of text of a blog post that is actually being piped in via another script. Also the script that I am piping into does something more than just convert that text to hex, but this is the basic idea never the less.

4 - The deal with a backslash

One of the options of the Linux echo command is the -e option that can be used to enable the processing of backslashes as a way to inject certain characters.

1
2
3
4
5
6
7
8
let spawn = require('child_process').spawn;
let processBackslash = process.argv[2] === '-E' ? false : true,
option1 = processBackslash ? '-e' : '-E';
let echo = spawn('echo', [option1, 'So this is a line \\n and another line \\n\\n'], {
stdio: ['pipe', 'inherit', 'pipe']
});