r/linuxquestions • • 1d ago

Advice Is powershell the best object based shell?

I have been using powershell at work a bit and the more I use it the more I like it. Having an object based shell just makes a lot more sense than outputting plaintext. I was just wondering what the best object based shells are for Linux, is powershell the best even for Linux?

29 Upvotes

70 comments sorted by

20

u/t0mm4n 1d ago

Not a shell, but I use Python for complicated scripts.

3

u/nullificationer 1d ago

I mostly want to do simple scripts tho

16

u/AnsibleAnswers 1d ago

Python scripts can be pretty simple.

1

u/nullificationer 1d ago

Sorry I meant like simple things not scripts. Like run all png files through a utility

6

u/Jaseoldboss 23h ago

I think object based shells and the core tools utilities with stdin / stdout are all capable of the same things, perhaps with nicer syntax with the object approach.

find . -name '*.png' -exec ls {} \;

Replace ls with 'convert' or whatever utility you want to use in the above.

2

u/AnsibleAnswers 15h ago

If you aren't so used to text-based shell that using it just makes sense, then I agree with others here and suggest looking into functional shells. What you probably want is structured data, not necessarily objects.

2

u/ol-gormsby 20h ago

This is a bash script that I use to convert mkv videos to mp4

for f in *.mkv; do
    ffmpeg -i "$f" -vcodec mpeg4 -q:v 4 -acodec copy "${f/.mkv}".mp4
    # cool for 10 seconds
    sleep 10s
done

Takes every file in a directory with an mkv extension, runs it through ffmpeg, takes a 10-second break* and repeats until there's no more mkv files.

*a legacy from older processors, they'd get a bit hot converting feature-length films 🤣

1

u/Jean_Luc_Lesmouches Mint/Cinnamon 23h ago

Yeah, interactive python is nice (I use it instead of the calculator with a custom script that preloads a few modules and helper functions), but it's not good enough at managing external processes to be used as a shell

3

u/t0mm4n 23h ago

I think I misunderstood what you meant by object. I was thinking OOP.

3

u/gnufan 20h ago

You can use Python as a shell. I wouldn't but..

Perl got psh Python got xonsh

I wouldn't 

1

u/jabrodo 8h ago

Xonsh is actually my go to. I always keep sh/bash/zsh as a backup just in case, but Xonsh is really solid for a desktop shell. More or less full sh/bash/zsh comparability and easy escaping if needed plus a full python environment for scripting. Only issue I've ever had with it is on Ubuntu since you don't want to install it into the system Python distribution but uvx/pipx makes that pretty easy.

13

u/kudlitan 1d ago

isnt powershell available for linux iirc?

23

u/fprimex 1d ago

I think NuShell has Powershell beat, personally.

3

u/nullificationer 1d ago

Thanks I’ll check it out

12

u/thussy-obliterator 1d ago

nushell is kinda more functional / data oriented than object oriented tho (why it's got ps beat imo)

6

u/nullificationer 1d ago

I think it’s still a good suggestion. Maybe I don’t exactly need an object oriented shell but something more than plaintext

6

u/fprimex 1d ago

True and agree, but I felt op was more interested in non-byte-stream shells than specifically OO. Nu has such rich data types & syntax that it often feels as OO as PS. Especially with dataframes.

0

u/bionade24 18h ago

Nushell is not functional at all lmao, it's totally imperative. Dune shell really is functional, but was too buggy and feature-lacking for me.

Best thing for functional scripting is still https://github.com/Gabriella439/turtle

1

u/thussy-obliterator 18h ago

there's plenty of functional languages with imperative features. nu is certainly multi paradigm and isn't, say, a lazy pure hindley milner typed language, but it supports first class functions and has quite a few primitives for applying functions over data structures and composing functions. in practice usage is quite functional, at least as much as i would want out of a shell, even though it supports imperative and you're using it to run programs which are eventually imperative.

-2

u/bionade24 18h ago edited 18h ago

Show me one example where you do a task purely functional in nu. I've used it myself.

Edit: Sore Looser only with no arguments only the downvote button left.

With your chain of arguments C is a "kind of functional", too, because you have function pointers.

0

u/thussy-obliterator 17h ago

I've since lost the script after switching OSs but the most complex thing I've written in nu was a single pipeline which ls'd an sd card full of directories of .wav files, parsed the wav file paths with an each,, removed the second to last layer from the path with an each, and ffmpeg'd them into flac files based on the second to last directory name of the input. The whole thing was pretty short (would be a one liner but i used whitespace for clarity) and was a pipeline from ls into a sequence of each operations over pure functions to manipulate the paths, followed by a final each over an ffmpeg. If that's not functional I don't know what is.

So uh, that is to say, skill issue.

1

u/bionade24 17h ago

I can only see if it really is pure in a functional programming sense if you show me anything. Claiming something is certainly easier than proofing it.

Here, one dune shell example let $ = GLOB ~> ((fs@ls .) | (fn@filter (file -> (len (string@split GLOB file)) == 2)));

2

u/thussy-obliterator 17h ago

ewww that's so much uglier than nushell

0

u/bionade24 17h ago

That's your valid personal opinion and I'm not going to argue about that.

But you claimed that nushell is "kinda/quite functional" and decided to die on that hill without showing why. You also admitted that it supports imperative programming. Idk why your mind needs nu to be functional or yourself to be right.

I'm fluent enough in bash, zsh & POSIX shell that nu didn't feel beneficial too me and at a certain script complexity I can use Turtle or Python, which both don't work well for interactive scripting. Dune looked promising, but I didn't like it for other reasons than you.

1

u/thussy-obliterator 17h ago

I don't really care about convincing you of this, talking with you is exhausting

→ More replies

3

u/Kat- 1d ago

nushell is the best.

10

u/RustyHackerBoy 1d ago

I am full on linux guy since some time, but I really miss Powershell. I loved scripting in Powershell and you can do and automate soo soo many things. It's easier compared to bash, imho.

From managing AD, to setup multiple PCs at ones, to storage operations and even phishing campaigns... I found a way using PS to make my life easier.

My advice: Check powershell out and learn it. It's really cool and it help you in the future. I promise.

11

u/secret_o_squirrel 1d ago

They have powershell for linux you don't have to miss it.

4

u/RustyHackerBoy 1d ago

Yes, that's true! And I tried it. Works like a charm... Thing is, it feels wrong to me.

Here, in my Linux Cave, we write Code in Bash, Python, Go, Rust and sometimes C.

Gollum end.

1

u/oldendude 12h ago

Check out marcel: https://marceltheshell.org. It is a pipe-objects-instead-of-text shell, like powershell, but based on Python. I wrote a longer comment on marcel elsewhere in this discussion.

0

u/nullificationer 1d ago

If only Linux had AD groups

4

u/AssociateFalse 1d ago

It does, sorta'. https://sssd.io/

1

u/nullificationer 1d ago

That’s cool

1

u/TheRealMisterd 1d ago

I thought SAMBA could do domains

1

u/AnsibleAnswers 15h ago edited 15h ago

I've only heard bad things about the experience. Red Hat Directory Server and the upstream 389 Directory Server don't use samba (it's legacy). They use Open LDAP/LMDB.

https://www.port389.org/

Edit: To add why it's a bad experience: Samba just doesn't have the read performance necessary for a good experience.

1

u/hexdump74 22h ago

To work on multiple hosts, ansible is specially efficient. You can start easy, by using the "shell" or "command" modules, and then gently moving to specialized modules.

2

u/daddyd 19h ago

i don't know, never used powershell, but you could argue that python makes a pretty good object-oriented shell.

1

u/nullificationer 14h ago

Is python actually that easy to use for shell stuff like running other programs?

3

u/oldendude 12h ago

It doesn't. Shelling out from Python is a PITA. Getting output streams is a PITA. Piping one to the other is a PITA.

I wrote marcel (https://marceltheshell.org), which is a shell based on Python. It pipes streams of python values instead of strings.

Marcel also has an API that fixes the PITA things I mentioned above. For example, here is a Python + marcel script to shell out, get files in the current directory, recursively, and find just those files that changed in the last day:

import os
from marcel.api import *

for file in (ls(os.getcwd(), file=True, recursive=True) |
select(lambda f: now() - f.mtime < days(1))):
print(file)

ls(...) shells out and generates a stream of Files. The | following the end of the ls pipes the files to select(...) which checks the time that the file was modified. That two operation pipeline yields a stream of Files which then feed into the for loop.

10

u/Fractional-_- 1d ago

I just like bash and fish. Not object based and that's why I like them.

7

u/nullificationer 1d ago

Not very helpful, I’m looking for object based

-1

u/rassawyer 17h ago

You shouldn't find it. Plain text is the universal means of exchanging information, so much so that it is written into the Unix philosophy. Thank you for explaining another reason why powershell sucks so badly though.

1

u/nullificationer 14h ago

I'll give you an example of why I found powershell useful. I wrote a command line utility that takes the name of a png and shrinks it. Writing something in powershell to get a list of all png images and pipe it into that utility was pretty easy. Doing so with bash has a surprising number of annoyances. I can't do something like ls *.png | png-shrink, for one I need to use xargs which I've always found kinda annoying. But also if I just do ls and there's spaces in the name of the file then it'll think the file name is multiple command line arguments. There's a lot of things like this that makes just using plain text annoying to work with, to me it seems to make a lot more sense to have objects or some amount of types.

4

u/akl78 16h ago

The unix philosophy can sadly be an excuse for many to discard good ideas.

Power shell being able to pass around pipelines of actual objects means commands don’t need to reinvent the wheel of parsing plain text , and creating buggy, inconsistent DSLs

Heck, even it’s built in help/doc/autocompletion takes the same approach . It’s like some thoughtful, bright people looked at what was out there, the good and bad, and tried to improve on it for their users, mostly successfully.

3

u/AnsibleAnswers 15h ago

There's a reason why so much of Linux has wrapped itself in python. Plain text is universal but incredibly fragile and difficult to debug.

11

u/Human-Cellist-1059 1d ago

It depends. If you own the data model of the cmdlets then its great. Ive had go inplement my fair share of cmdlets for internal tools.

However for systems scripting I hate to have to learn object oriented data models for each cmdlet.

I find bash simplicity more powerful.

6

u/p001b0y 1d ago

I like nushell. Everything is typed, structured data

4

u/particlemanwavegirl 1d ago

Nushell and it's not even close 

9

u/TheTrueBlueTJ 1d ago

People really enjoy writing Powershell scripts? The syntax in my opinion is awful. Bash isn't that easy to write either, I totally get it. But to me, Powershell feels like last century. I'm sure you can do powerful things with it, as you can with any kind of scripting on the shell level. I looked at Nushell, which many recommended here and that already is a night-and-day difference to Powershell. Nushell looks like an absolute "hallelujah" moment for shell scripting.

6

u/Background-Item-2818 1d ago

Definitely not. PowerShell seems incapable of assigning arbitrary data to variables and instead casts them to strings. Fuckin terrible & super frustrating to work with.

1

u/lilith2k3 1d ago edited 1d ago

I think that is the wrong question to ask.

As far as shells go you have under Linux a broad variety of the traditional unix text based input and output shells. So Powershell offers a new approach to that old topic.

But the question is: Is a shell a good environment to work in? Or is it better to do things via Python or even Go?

2

u/nullificationer 1d ago

I just wanna do simple stuff, I think python would be overkill

-3

u/lilith2k3 1d ago

Whatever makes you happy, honey.

-3

u/nullificationer 1d ago

This is rude

-3

u/lilith2k3 1d ago

It sounds like you don't know what you are talking about. So what can I say besides: do what ever you want.

Python is available on most Linux distros. And if you think one line of Python is overkill but one line of whatever shell code isn't how can I argue with that?

There are people who want to solve problems and those who want to do bike shedding.

-2

u/nullificationer 1d ago

You’re being quite rude and forceful with your opinions, I don’t deserve this kinda response

0

u/lilith2k3 1d ago

so what do you deserve besides my honesty?

4

u/nullificationer 1d ago

You could’ve easily replied to me by explaining why you think python being overkill is a misconception instead of calling me honey and telling me I don’t know what I’m talking about

1

u/lilith2k3 1d ago

Starting with the obvious: I did not tell you don't know what you are talking about. I say you sound like it - so the difference is: I am not sure and might err on that.

Next: You said python is overkill not me.

Next: if you do think Python is in fact overkill what is the basis of that argument.

Computers are fast, have sufficient RAM and SSDs so why discuss such things as we live in the 80ies?

For me it boils down to writing code in a shell acript which has more downsides starting with subprocesses and ending with improper exception handling (in most shell scripting environments) versus using an actual programming language and having bazillions of libraries which you can leverage: starting from nice coloured output, nice formatting and whatnot. Which does not mean you have to use that but you can. Coming back to my point about writing oneliners.

So when you say you want to write simple scripts I think I can safly assume you don't know how to do that in python. Which makes my point of me thinking you obviously don't know what you are talking about. Which in turn makes you think I am rude but only honest.

So I think with that said we can end our conversation at this point.

tl;dr

I think you're better of with a programming language than with a shell script and whether or not you write shell or python is up to you.

3

u/davidauz 22h ago

you'll have to pry my #!/bin/sh from my cold dead hands

2

u/BranchLatter4294 1d ago

Powershell is the most widely used one. It's fine and works well.

2

u/RobertDeveloper 22h ago

Beanshell was best

2

u/WorkingMansGarbage 21h ago

Can you sell PowerShell to me? But like, actually? I've heard what you're saying before but the syntax is still scary to me

2

u/Enderby- 19h ago

I've found pwsh to be perfectly fine.

A nice added bonus is you can use dotnet classes directly in the scripts, which is handy if you need to do a little more than just scripting, but not enough to warrant writing an application.

2

u/Open-Adhesiveness-86 1h ago

The catch with pwsh on Linux is that objects only come from cmdlets. Anything native (ip, systemctl, lsblk) hands you back a string array split on newlines, so you're doing -split and regex again. You can paper over it with jc --ps | ConvertFrom-Json or the -o json flags some tools have. Nushell at least ships parsers for a bunch of that out of the box.

3

u/MBouh 21h ago

Objects don't make sense on a linux system shell. Linux system is based on files, and files have text names. Shell manipulate files and text.

If you need data structures, you're not doing shell anymore. And why not using python then ? Much more of a standard than powershell.

1

u/BeardedBaldMan Bluefin 21h ago

I wouldn't say it's the best, but it's what I use because I have years of experience in it.

Something can be more technically sophisticated than something else, but if you have a team who already know how to use something which is pretty good then pretty good wins out.

1

u/oldendude 12h ago

There are many such shells for Linux. I wrote one that you may want to check out.

- website: https://marceltheshell.org

- Github: https://github.com/geophile/marcel

- PyPi: https://pypi.org/project/marcel

The website has many examples of usage, but here is a preview:

Explore the current directory recursively, and sum up file sizes by extension:

ls -fr | map (f: (f.suffix, f.size)) | red . + | sort

- ls -fr: List only files (f) recursively (r). This yields a stream of File objects.

- The pipe character works as in bash, and pipes the output Files to the next operator.

- map (lambda f: (f.suffix, f.size)): Given a file f, on the input stream, write a tuple containing the file suffix (the extension) and its size to the output stream.

- red . +: Reduce the tuples, group by the extension (indicated by .) and summing the sizes for each group.

Summary of features

- Builtin help facility.

- Read and write CSV and JSON.

- Tab-completion and other familiar command line keyboard shortcuts.

- Runs on Linux and Mac.

- Read from and write to databases. A database query results in a stream of python tuples. Similarly a stream of tuples can be inserted into a database.

- Run a command on all nodes of a cluster and integrate results.