In the world of shell scripting, Bash has long been the go-to language for automating tasks and glueing together various command-line tools. However, Bash’s quirks and limitations have left many developers yearning for a more modern and expressive alternative.
Enter Amber, a new programming language that compiles to Bash, bringing the power of high-level programming to the realm of shell scripting.
“Amber is not a wrapper of Bash as implementing new data types and error handling won’t be possible using a wrapper“, said Paweł Karaś, creator of Amber, in an exclusive interaction with AIM.
High-Level Scripting Language
Though Amber operates similar to Bash, it is designed for frequent command execution.
“Amber provides built-in error handling capabilities, making it easier to write robust scripts,” said Karaś.
The new programming language is designed specifically for the scripting style where executing external commands and manipulating their results is integral.
In Python, executing a command and capturing its output requires several lines of code and explicit error handling. Amber, on the other hand, provides a concise and intuitive syntax for command execution, along with built-in error handling capabilities.
One of the standout features of Amber is its focus on runtime safety. Almost all operations available in the Amber language are safe.
The exceptions are commands that refer to the Bash shell, which can fail, and failing functions that can fail at any time through error propagation or by explicitly using the fail keyword.”
Amber requires developers to handle errors whenever a command or failing function is called. This can be done by propagating the error upwards, handling it in a dedicated failed block, or marking the command as unsafe if the developer is certain it will succeed.
“This approach ensures that scripts written in Amber are more robust and less prone to unexpected failures,” Karaś emphasised.
Long Way to Go
One concern among potential Amber users is the performance impact compared to vanilla Bash. As there’s no way to handle float values in Bash, Amber has to rely on bc a calculator for Bash, which means that Amber requires running an external program every time to perform an arithmetic operation.
The team behind Amber is planning to create a new type of data called Int
that will use Bash’s built-in ability to do math with whole numbers (integers). As Bash can’t handle decimals, they will also look into better ways to deal with Num
, which is the data type Amber uses for numbers with decimals.
Meanwhile, Amber’s syntax has raised some eyebrows, particularly the use of >
instead of #
for comments. Karaś clarified the reasoning behind this choice, stating, “Amber is a language designed to resemble the syntax of ECMAScript, where comments start with //
”
The idea behind this choice is to give users the feeling of writing in a language that looks familiar. This decision aligns with Amber’s goal of providing a modern and intuitive programming experience for Bash scripting.
A Reddit user asked why Amber is not directly compatible with POSIX shell, which is a standardised command-line interface and scripting language that ensures compatibility and portability across different Unix and Unix-like operating systems, especially in the era of the minimal container images where POSIX shell can have the lightest posix-compliant shell possible.
“We are considering creating a compilation flag for maximum compatibility, such as --output=sh
,” Karaś explained. “Amber would then return errors when syntax unavailable for the given format is used. This idea is being developed and may evolve into a better solution.”
Karaś also touched on Amber’s ability to handle spaces in file names, a common pain point in Bash scripting. He assured us that Amber’s outputted code will soon be Shellcheck verified, ensuring proper handling of file paths and other string interpolations.
As promising as Amber’s potential seems, one must remember that it is still in the alpha stage, and it is not recommended for use on production servers. Improvements are still being made and developers are currently working on a standard library so as to not worry about interpolating file path to open a file.