Bash vs. Python vs. JavaScript: Which Is Better for Automation?

ARTICLES 30.10.21 30.10.21 168
Бесплатные курсына главную сниппетов

Comparing the pros and cons of Bash, Python, and JavaScript-based Shell scripts

A Shell script refers to a source file that is specially created for a command-line interpreter like Bash. Programmers often write Shell scripts to boost productivity by automating tasks they do repetitively. For example, they write automation scripts for file manipulation, environment setup, running test suits, and deployments. Also, we can use Shell scripts inside virtual machines or CI/CD services to have clean and configurable test runs or deployments. Shell scripts typically run on a command-line interpreter. However, we can write Shell scripts with popular general-purpose programming languages and still call them Shell scripts.

For example, you can use Python to create modern Shell scripts and execute them with the Python interpreter like a Bash script. You may spawn other Unix processes inside your Python source file same as you do with Bash. Also, JavaScript is a great alternative to write automation scripts quickly, thanks to the Nodejs runtime.

You can indeed write Shell scripts with Bash, Python, and JavaScript. But, like any other technology in software development, these technologies have various pros and cons. In this story, I will explain how you can use each language to build Shell scripts with the pros and cons. Moreover, I will explain how to select a suitable language for your next Shell script.

Every popular operating system typically offers a CLI (Command Line Interface) via a terminal application. A terminal application runs commands via a particular command-line interpreter such as Bash, Z shell, C shell, and KornShell. We can put specific commands to a file and execute it via a preferred command-line interpreter. In other words, we can write Shell scripts with Bash, Z shell, etc. Bash is a widely used command-line interpreter because it’s included in almost all Unix or Unix-like operating systems by default. Therefore, you can write portable POSIX scripts with Bash.

Bash lets you write Shell scripts with minimal grammar. If you need to execute several commands, you only need to put those commands in a Bash script line by line. Bash also supports some basic programming concepts — such as if-else statements, loops, arithmetic operations, functions, and variables. Bash natively support processes. In other words, you can spawn other binaries as commands. For example, if you need to execute the ping binary, you can write the ping command in your Bash scripts. There are several ways to show GUI with Bash scripts too.

Bash is a command language, not a general-purpose programming language. Therefore, when your automation script’s logic grows, it becomes more complex and not readable. Also, Bash always tries to treat everything as commands because it’s a command language. See the following example:

#!/bin/bash
x=10
echo $x # prints 10
x = 10 # x: command not found
echo $x

Bash has no standard API, but it comes with simple built-in features (Eg: built-in text manipulation). However, you often have to spawn processes to process data (Remember sed?). Therefore, Bash performs very slow compared to other languages that we can use to write automation scripts.

Python is a popular alternative for Bash to write environment setup, build, and release scripts. I saw that the Electron project uses Python for several utility scripts. You can’t run commands directly in Python because it’s not a command language. But running commands and capturing output is a piece of cake with the subprocess module. Look at the following example:

#!/usr/bin/python3import subprocess
o = subprocess.check_output(['node', '--version'], text = True)
print('You are using Node ' + o)

The above Python script prints the current Nodejs version of the computer. That script is equivalent to the following Bash script.

#!/usr/bin/basho=$(node --version)
echo You are using Node $o

Now you can write modern rich Shell scripts by using built-in features of Python. But, the Python interpreter doesn’t natively support process execution as Bash does. Therefore, if we need to simplify the Python script further, to make it look more like Bash, we have to use a tool like Shellpy. Look at the following Shellpy script that does the previous script’s job:

#!/usr/local/bin/shellpy3o = `node --version
print('You are using Node ' + o.stdout)

Python is a very developer-friendly language. Also, it comes with many useful built-in libraries. The Python interpreter is preinstalled into almost all Unix-like operating systems. Therefore, Python is also a good alternative for writing portable automation scripts.

But Python has a slow program execution time compared to other languages. When you use some libraries, Python performs too slow even compared to Bash. Tools like Shellpy and Plumbum offer convenient APIs to work with processes and commands. But, still, we have to set up and write some additional code.

As we know, JavaScript was initiated as a scripting language for the web browser to make web pages a little bit dynamic. The Nodejs project took JavaScript out from the browser sandbox to the operating system level. Nodejs offered the child process API to spawn new processes. Also, Nodejs introduced several cross-platform APIs to work with file handling, network, and console. Now, JavaScript and Nodejs both include all the features that are needed to make modern automation scripts.

Nodejs doesn’t natively support binary processes like Python. But, you can run other binary executables via the child process API. Look at the following example that shows the processor architecture:

#!/usr/bin/node
let exec = require('child_process').exec;
exec('arch', function(error, stdout, stderr) {
console.log(`Your computer's CPU architecture is ${stdout}`);
});

The above Nodejs script doesn’t look like a Shell script because of the callback function. Therefore, you have to do some additional work to make it look like a simple Shell script. Google’s zx project was recently introduced to solve this issue with JavaScript-based Shell scripts. We can indeed simplify the syntax of the above code and write the following script with zx.

#!/usr/bin/env zx
$.verbose = false
let arch = await $`arch`
console.log(`Your computer's CPU architecture is ${arch}`)

The zx tool provides almost all the features that you need to write Shell scripts. JavaScript’s native JSON support is a great feature to make rich and modern Shell scripts. But, every CI/CD engine and popular Unix or Unix-like operating system don’t provide the Nodejs runtime and a package manager by default.

There are numerous general-purpose programming languages out there. Even though we rank those languages based on popularity, we can’t rank based on overall features. If there is such thing called the best programming language, programmers always use one programming language to build everything. However, we can select a suitable programming language according to our requirements.

Therefore, you can pick Bash, Python, or JavaScript to write Shell scripts based on the following facts and conditions.

 

на главную сниппетов
Курсы