Linux echo command and scripting with nodejs
The echo command can have many real would use cases also though, for example when writing a bash script I might want to print some result in string form to the standard output when the script is called, one way to do so would be to use the echo command and pass that string value as the first argument. When doing so there are a number of options that can be used to set of there should be a line break at the end or not, or if some special characters should be used or not to create the final output.
So in this post I will be going over some basic example of the Linux echo command, and also go over some of the advanced options that often prove to be useful when using it.
1 - Some basics of the Linux echo command by itself
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.
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.
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.
1.3 - make it so echo will not add a line feed at the end
By default the echo command will add a line feed character at the end of the output. Most of the time I will want to have a line feed at the end of the output that echo spits out to the console, however in some cases I might want that to not happen actually. For these kinds of situations there is the -n option of the echo command that I can use to make it so the echo command will not append that final line feed charterer.
1.4 - Piping example
One major use case example of using the echo command is to just create some test standard output when it comes to playing around with a new command to learn what it can do when it comes to feeding it something with the standard input. For example take the base32 command, this command can take some input from the standard input and spit out a base32 form of that input. However in order to test it out I need to have some input to pipe into it and one way to do so would be to use the echo command.
There are other options of course, for example if I had a text file that I wanted to pipe into it I could use the cat command to do so. However when it comes to doing something like this I might also want to write the results of it to a file also, and with that said I think I should move on into another Linux echo example that make used of redirection.
1.5 - Redirection example
Linux Bash Redirection is another feature of bash that I think I should cover here while I am at it. With that said the Linux echo command combined with redirection can be used as a way to create text files from the command line. The echo command can be used as a way to generate some standard output, and then redirection can in turn be used to create a file with that standard output.
2 - The Linux Echo command nodejs examples
2.1 - Using spawn in child process to call the Linux Echo command
So first off a basic node script where I am using the child process module as a way to call the Linux Echo Command. However I will also be showing the two general ways to go about spitting something out into the standard output when it comes to nodejs scripts also here which are what should be used in a nodejs script.
2.2 - 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.
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.
2.3 - 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.
3 - Conclusion
Well that is it for now when it comes to the Linux echo command, but there really is only so much to write about when it comes to this topic. The echo command is just one way to go about creating some standard output from the string value that is given to it as an argument. So as the name suggests it just echos what is given to it as an argument to the standard output of the console.