Can Rust be used instead of Bash?

Hey everyone,

I've collected a bunch of Linux OS tweaks to have it function better for my needs.

I need a way to remember exactly what those tweaks were and then to be able to reimplement them should i reinstall my OS in the future. Also, I can share them with others too.

So i started to put all my tweaks into a bash file, but it's starting to get a bit heavy handed, and i was looking for a way to split all the tweaks into sperate modules/files and the internet keeps telling me that at this point maybe my needs have outgrown Bash, and to look for alternatives such as Python... But I love Rust!

So can Rust help me here? Can it execute 'terminal commands' and read input?

I understand programming languages to be good at editing data owned by the program itself, but... modify data owned by the system, can that be done?

It's mostly just editing text files scattered around the system, with some use of regex, grep, sed and awk, here and there.

Any thoughts?

I'm honestly thinking that Bash is probably better here because it's just clearer what the intent of the code is, will be fewer lines, and especially for sharing, people aren't going to want to run a binary as root that modifies the OS if they aren't very familiar with Rust and spend the time combing through the source code.

Yes, you can run commands using Command.

@alice, do you know absolutely everything about computers and Rust???

1 Like

No, for example, please don't ask me questions about GUI.


Alrighty then! :slight_smile:

In any case, for you and anyone else reading this, now I understand that it can be done, I'm looking for guidance as to if it should be done! Cheers

As for whether it should be done, I will just say that Rust is not the easiest language to do it in.

1 Like

If I were to approach this problem with Rust, I'd want to have a runtime configuration file and an interpreter written in Rust. Over time, that configuration file would probably turn into an sh-like programming language anyway which feels counter-productive.

On the other hand, a shell language that's explicitly designed to be non-interactive might be an interesting project to explore.

1 Like

Seems there is a growing community of people wanting to do this kind of thing. For example Rewritten in Rust: Modern Alternatives of Command-Line Tools

Which I notice now includes a shell alternative written in Rust nushell

1 Like

If you want to be pragmatic about this, I recommend using Ansible (which is Python + YAML).

For general scripting in Rust, take a look at the xshell crate.

I've long since started to move any bash scripts in my Rust projects to Rust by using cargo-xtask. In my experience it is much much easier (if more verbose) to write a small Rust program than to write an equivalent shell script, even when using ShellCheck.

I'm want to do things like more interesting menus, proper error checking and undo the things that went right in a chain that ultimately fails. Not just exit on faliure with an echo if you're lucky! which is all bash is really good at to be frank

Thinking about my use case (tweaking my OS after a fresh install) i would have to write (and execute) a bash script to install and run my Rust program to get around having to use bash, which is kind of hilarious

For project-specific build automation, I strongly prefer Rust over bash+yaml. It's easier to modify, execute locally (and on windows) and share configs that way. You do pay for it in increased verbosity, but it is wort it for me. To smooth out the experience, there are xtask, xshell and xaction.

For user-global config of a Linux box, I use NixOS which solves the problem at the root, + Julia for scripts (example). I've been looking for bash replacement for a long time (including writing my own Python DSL) and, counterintuitively, Julia is the perfect language for this (Shelling Out Sucks).

1 Like

Is there any video tutorial or something out there that could guide me through this sort of stuff?

This is really a new way of thinking / doing things for me

There's also the subprocess crate, which might be a little nicer for shell-like scripting.