r/lua • • May 03 '25

`shelua`: use your shell as real lua code!

Announcing: https://github.com/BirdeeHub/shelua

It lets you do this:

print(sh.ls '/bin' : grep "$filter" : wc '-l')

or

print(sh.find('/usr/bin', '-type', 'f', '-executable') : grep 'python' : wc '-l')

Some of you may have heard of https://github.com/zserge/luash before.

I heard about it last week.

"What a beautiful little library!" I thought to myself, and went to try it out.

That's when it hit me. "I'm sorry, it does WHAT to _G?!?! I can't import this anywhere!"

I also found out error codes don't work before 5.2. And that it can't do real pipes. To be fair, it seemed like real pipes could not be done at first to me as well.

I look at the repo. Last push, 9 years ago. This was made for me. A cool experiment neglected.

Within the evening, I had it localized to the variable it came from, without losing its ergonomics, and fixed error codes prior to 5.2 and added some useful settings not there previously.

But I was not satisfied. I wanted REAL pipes. The ones in bash where all the commands start at the same time.

And its kinda fun... I might want to use it with another shell...

Well, a few days later, now you can enable proper pipes, and you can even make it work with any shell with just a bit of effort.

It is still tiny, and a single file.

I am pleased to be able to announce to you all an exciting and far more modular iteration of the idea luash brought to us. One you can include in other projects or even your neovim configuration without fear of messing it up. I have really enjoyed it so far.

https://github.com/BirdeeHub/shelua

24 Upvotes

24 comments sorted by

View all comments

Show parent comment

2

u/no_brains101 May 03 '25 edited May 03 '25

oh, filter is a variable defined in lua. MB if that wasnt clear.

I pulled it from the readme where it is more clear

-- $ ls /bin | grep $filter | wc -l

-- normal syntax
sh.wc(sh.grep(sh.ls('/bin'), filter), '-l')
-- chained syntax
sh.ls('/bin'):grep(filter):wc('-l')
-- chained syntax without parens
sh.ls '/bin' : grep filter : wc '-l'

OH hang on I know how to make it make more sense and only add 3 chars 1s+

Theres an escape_args setting but by default it does not escape.

2

u/Amablue May 03 '25

I still don't understand :/ (What does MB stand for?)

local myvariable = 10
local tbl = { myfunc = function(self, x) print(x) end }
tbl:myfunc myvariable
stdin:1: function arguments expected near 'myvariable'

How are you getting away with omitting the parentheses?

2

u/no_brains101 May 03 '25

you are correct. Readme code OP on that one. The rest do work but yeah that was (inherited) readme bug. Will correct.

3

u/Amablue May 03 '25

Okay that makes a lot more sense then. I was going crazy trying to figure out how that could possibly work and I thought I was going insane.

1

u/no_brains101 May 03 '25

yeah just the classic "cant easily run a readme"

1

u/no_brains101 May 03 '25

here is a fun one tho

This one does work

print(sh 'type' 'ls')

1

u/AutoModerator May 03 '25

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/no_brains101 May 03 '25

MB is my bad.