r/lua • u/WorkingMansGarbage • May 19 '26
Discussion Is Lua fit to use for general, personal use scripting of basic things (as replacement for Bash)?
Sometimes I have to automate a basic task, such as running some commands on files in my music library, bulk renaming, moving things, etc; and like anyone, I tend to try and use Bash for things like that, because that's what I was taught. The problem is that I don't like Bash scripting at all and I have to look up the stupid syntax for every little thing every god damn time. From what I've seen, other shells aren't too much better in my opinion, and in general, I actually don't like relying on shell commands within my scripts for things that would be simpler in a 'normal' programming language.
I've tried using Python as a replacement, but I don't like having to make a venv. It's bulky and annoying. I'd like to just have one script file I can run in one command whenever and not have to go through hoops.
I've been eyeing Lua just because it sounds cool but I've never had a reason to actively learn it. Would it be fit for this usage? Or is it solely a language for "project use", so to say?
3
u/Emerald_Pick May 19 '26
I'm confused about needing to set up a venv to use python. Can't you just make the Python script executable and then use a
#!/bin/pythonshebang to make it work just like a shell script?As far as Lua goes, I've been annoyed by its OS and file management commands. There're libraries to make it nicer, but without them I've needed to resort to calling the OS's
lscommand to get a list if files in a directory. If you're doing things like converting a folder of images from one type to another, it's a little clunky in default Lua.But IMO it's way nicer to read and work in than bash for everything other than calling OS commands.
7
u/immortalx74 May 19 '26
I don't know about bash specifically but I've used Lua scripting for tasks like these in Windows as a replacement for cmd. I do heavily use os.execute in those scripts to call external utilities and also use Windows' own facilities, but it's the combination of having Lua tables and string manipulation which I find better than pure cmd.
5
u/antara33 May 19 '26
Yes, it is. You can do almost everything with LUA aside from multithreaded stuff. And even that can be done with some creativity.
Tables provide the possibly most ergonomic data structure to fit all your needs too.
4
u/SoCalSurferDude May 20 '26
Here's a tutorial that shows exactly what you are asking. The tutorial is using mako, but you can just change this to plain old Lua
https://makoserver.net/articles/Using-The-Mako-Server-and-Lua-Instead-of-Bash-For-Automation
3
u/mykesx May 19 '26
Fish shell scripting is excellent. But you need fish installed to use it for scripting.
3
u/fuxoft May 19 '26
Yes, very. I use Lua extensively when scripting on Linux. "io.popen" is your friend.
3
u/Old_County5271 May 20 '26 edited May 20 '26
No. (No-ish)
Whenever you os.execute or io.popen you are running the system shell, which is /bin/sh in posix or batch in windows.
So you are always dropping down to the shell.
Not only are you always dropping down to the shell, but you are launching a new shell for each os.execute() meanwhile, if you just write a shellscript, you fork once and done, not only that, but you can (in shell) do something like launch lua, and pipe a file to it, it can write a file which you can read, avoiding forking altogether, forking only once each.
In order to use Lua well enough you need Lua rocks or Lua power or the like because Lua has no batteries. So it's actually worse than python/perl/TCL/js in this regard as well.
2
u/xoner2 May 20 '26
Yes.
Some tips:
You'll want to write or copy some utility functions:
for file in io.glob 'some-dir/*.txt'io.popen, e.g.:for line in io.plines 'pslist -t'Following C libs needed:
1
u/drcforbin May 19 '26
If you're writing a cli program that does something itself, it's fine, but if you're tying together other command line tools it's probably going to be a poor fit. That kind of thing really is easiest in a language designed for that (like a proper shell, or even perl)
1
u/hawhill May 19 '26
when you know Bash and Python, learning Lua is a no-brainer, and you'll realize early on that it is not what you are searching for (the odd "I made a Lua replacement for the shell" project nonwithstanding).
Yes, Bash is a bit awkward, but then it is always there on any machine you encounter. Well, close to that, at least.
1
u/GanacheUnhappy8232 May 19 '26
I recently use schemesh
https://github.com/cosmos72/schemesh