85

My primary use case for Amber is when I need to write a Bash script but don't remember the silly syntax. My most recent Bash mistake was misusing test -n and test -z. In Amber, I can just use something == "" or len(something) == 0

you are viewing a single comment's thread
view the rest of the comments
[-] IanTwenty@piefed.social 7 points 5 days ago

Interesting. There is this example in the docs:

let result = $ cat file.txt | grep "READY" $ failed {
    echo "Failed to read the file"
}

https://docs.amber-lang.com/basic_syntax/commands

How can we know for sure what failed here? Was it the cat or the grep? My instinct says the pipe returns the code of the last cmd or failure, which could be either.

Perhaps it's just a contrived example and it would be better to separate testing file existence from grepping in real code...

[-] lens0021@programming.dev 3 points 4 days ago

Yep, the code you provided is compiled into this:

command_0="$(cat file.txt | grep "READY")"
__status=$?
if [ "${__status}" != 0 ]; then
    echo "Failed to read the file"
fi

So, the outcome would depend on the pipefail option. (set -o pipefail)

As you suggested, an Amberic snippet would be:

import { file_read } from "std/fs"
import { match_regex } from "std/text"

const result = file_read("file.txt") failed {
    echo "Failed to read the file"
}
if match_regex(result, "READY"):
    echo "file.txt contains READY"
[-] IanTwenty@piefed.social 2 points 4 days ago

Thanks for that, makes sense. I like that Amber gives the ability to code more defensively/robustly where appropriate but can also get out the way if you just need to run a bunch of BASH raw.

this post was submitted on 02 Dec 2025
85 points (100.0% liked)

Linux

10467 readers
662 users here now

A community for everything relating to the GNU/Linux operating system (except the memes!)

Also, check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 2 years ago
MODERATORS