229 points · by indigodaddy · 2y ago · 185 comments
I still love bash and use it as well. But the hate powershell often gets is misguided.
PowerShell 5.1 “UTF8” actually meant “UTF-8 with BOM”
PowerShell 7 “UTF8” meant no BOM.
If you read a file in PowerShell that does not have a BOM it decodes it as ANSI. Yes, ANSI, not Ascii. Which is a hilarious choice because you can’t even write an ANSI file in PowerShell. The closest encoding you can use is “Default” which will use the system default but your systems default might not be ANSI.
PowerShell is a hot mess. Go read the official docs for character encoding[1] if you don’t believe me.
I have CI scripts that have to use .NET calls[2] in PowerShell to write ssh keys to disk because it’s impossible to write utf-8 no BOM (or ascii) with out carriage returns (\r) being inserted before every new line (\n) even when the incoming string didn’t have them.
File encoding is just ONE of the reasons I hate PowerShell and why it’s so obviously clear people who were not qualified to design a shell, designed a shell.
[1] https://learn.microsoft.com/en-us/powershell/module/microsof...
[2] https://stackoverflow.com/questions/5596982/using-powershell...
I can't speak for Jeffrey and his team, but I feel like a lot of their decisions came from trying to get corporate behind the shell and present it as a shell for Windows. They avoided picking political battles outside of their main goals (a modern shell to replace cmd.exe and the object-based pipeline model). What we've got are a set of decisions that aligned with Windows and Microsoft practices of that day and age:
Microsoft is focusing on .Net as their general platform? We'll implement PowerShell on .Net.
Windows's standard for Unicode text files is either UTF-16 LE or having a BOM for any other Unicode transformation? We'll do UTF-16 by default and always add a BOM if you choose UTF-8.
Windows is using CRLF? Well, we'd pipe CRLF-delimtied text by default.
Visual Basic and C# programmers expect functions to have names like "GetChildItem" instead of something like "dir" or "ls"? No problem, we will set the canonical command name to be a long, programming-language-like name and set up aliases that look (but don't behave) like Unix and cmd.exe commands.
The result was not pretty, but I still appreciate the ideas we got from PowerShell. nushell took these ideas and implemented them in a more modern way.
[1] https://corecursive.com/building-powershell-with-jeffrey-sno...
It doesn't seem like it could be for backwards compatibility, as now an old script that writes a file with defaults can't later read it with defaults?
I find that unfair towards their creators. They invented a truly innovational shell, whereas everybody else continued the text-based approach. The object-oriented approach is so much simpler when it comes to process results of Get-ChildItem (ls) or Get-Process (ps) when you get objects with properties, instead of just text. Especially when in text-based shells it matters how you call e.g., ps (aux or -efH or whatever).
Now, I find your statement unfair, because in Linux world, you just create a new shell and whoever wants to use it, can.
At Microsoft, a successor to cmd.exe had to be shipped with Microsoft, otherwise it would never have been adopted. Most big companies would never allow a third-party open-source shell on their Windows servers. Therefore, you must navigate a big ocean of politics and powers, guarantee nackwards-compatibility and meet expectations of thousands of companies. This inevitably leads to behaviour like encoding that is frustrating to use. Until you read the docs, which state quite comprehensively what you have to expect - as you discovered yourself.
Edit: yup, just did small cmd line client and all works. Thanks.
As a comparison (to most of us semi-knowledgeable windows / powershell users), ask any semi-knowledgeable Linux user about the difference between Ksh/Bash (or that weird one that Ubuntu pushed and aliased to one of the above for extra confusion). Or ask them if any of them remember how tf to use SED or AWK without looking up the docs because those damn things are made by satan in the depths of hell. Or how to use parameters inside shell scripts, and the various incantations they have to slice and dice the params.
One thing I will give Powershell credit for is, back in 2012, being able to chuck something together when I did not have the development tools I needed. I knew that Powershell was .NET under the hood so I had a stab at being able to load my DLL files and calling methods inside a class. It did the trick! I was able to hand it over, suggesting they use it as a Task Scheduler (daily) and hardly anyone touched it during the remaining of that promotion (4 months)
Despite all this, if I ever have to write a script for odd execution or regular automation... I wish I could use bash. I have mentioned Git For Windows or using WSL but I just cannot win anyone over. Its not like anyone is an expert on shell scripting.. powershell or whatever.
As much as I like Powershell -- I still consider bash to be better. I think half of this reason boils down to what you are accustomed to. I have been using Linux properly since.. around.. 2006.
I just think coming from a Linux background, it's very hard to wrap my mind around the mindset of PowerShell which is just quite different from Bash.
If you had to write something a little more complicated, I would consider F# in many ways than Powershell especially if I wanted the .NET tools.
Of course, there is python.
I've written in a lot of different languages in my time and never had this issue with anything else. It's weird.
people who rip on powershell but prefer bash make no sense to me at all
e.g. Get-ChildItem is aliased to "gci", "ls", and probably "dir" as well.
You can do what ever you want for innovation but if you choose mindf*ck as your UI..
It's about what twenty people could sling together and make work on a Vax in the 80s in 4Kb.
Powershell has regular syntax, legibility, a high degree of discoverability, and included batteries for things that bash victims reimplement again and again.
It may not be the best shell for your purposes - it certainly won't run in 4Kb - but it wipes the floor with all legacy shells for usability.
You're welcome to have an aesthetic opinion on the syntax, but you must be dead inside if you can look at an average bash script and not recoil in horror. Argument parsing? sed? Control statements? Quoting and escaping?
"It's all about the user" is it fuck, you're on drugs.
> I’m more comfortable with bash commands than CMD or PowerShell commands (because I’ve been using Bourne shell (sh) and friends for more than 30 years).
This is the only argument I'm willing to accept, and honestly it's the only argument the writer needs, really: un-learning 30 years of experience to use something else even if the latter is objectively superior, would be a pain in the neck for anyone.
That being said I believe that seriously attempting to use the native tools would help the author understand Windows at a more fundamental level.
Also, this does raise a few questions, though—if the writer has been using UNIX shells for that long, why are they using Windows at all? As I commented elsewhere, why shoehorn the whole set of UNIX core utilities and binary utilities onto Windows when the latter comes with its own shell, command-line utilities, and management tools? More importantly, the author going by the rest of their posts (assuming it was the same author) doesn't strike me as the type (i.e. normie, for lack of a better word) to use Windows as a daily driver, but they seem to use it all the same. Therefore, why not an OS where these utilities feel more native and more useful, like Linux or one of the BSDs?
In contrast to the author I've been using and programming almost nothing but Windows (with a brief stint on Linux in university and now at work, but only for work), and on Windows I use PowerShell and if I really need to, CMD.exe (good riddance). As far as compilation goes, I use Visual Studio with MSVC and Clang-Cl, because they are the only complete development environment for Windows—all the others (MinGW, Cygwin, MSYS2) are incomplete or worse in some way, or make life harder than necessary in some way.
> I have experience using Windows PowerShell (powershell.exe), but do not yet have experience using the cross- platform PowerShell Core (pwsh).
For the record, functionality between powershell.exe and pwsh.exe is almost identical, with some minor differences[1]. Additionally they differ in the .NET runtime they were written against (.NET Framework 4.8 for powershell.exe, and .NET 7 and later for pwsh.exe).
[1]: https://learn.microsoft.com/en-gb/powershell/scripting/whats...
I'm a Linux user and developer who writes software targeting Windows.
Tools like Git Bash and MSYS2 let me write one set of build scripts that work across Windows, macOS and Linux, because all of the tools exist on the three platforms.
I don't have a need to understand Windows on a more fundamental level, nor do I want to. I just want to meet my users where they are at, which is Windows/macOS/Linux.
YMMV
Powershell is cross platform too these days, which makes this kind of a moot point.
Powershell on Linux isn’t worth is because I end up having to parse everything as text anyway, completely negating any benefit I can come up with. It doesn’t feel as natural as the *nix tools to me.
I also have no desire to use Powershell when I don't need to.
With bash, you only need to learn 10 or 20 short commands and a few flag variants, over a gently long period of time. Then you're all set for 97% of cases likely to be encountered.
And bash is still a lot better as a shell language. Powershell commands are long and annoying. If you remember all the aliases (most of which are annoyingly bash commands but hardly behave that way) then the parameters are long and annoying.
Stringing commands together requires too much API digging because objects mean you need to run a variety of incantations on the results to know what members you need to pull out and what data format they have, etc. in Bash it’s always strings and if you know a couple of useful string manipulation commands such as cut, that you probably know anyways for other reasons, you can get by with stringing most commands fairly trivially.
Powershell OTOH is a lot better when I’m trying to write an actual reusable script. But at that point why wouldn’t I just use something like Python anyways (or if it will be associated with a front end repo I will often just use nodejs since it’s definitely installed). Python has much more usable comparison, control flow, etc operators, much bigger libraries, better multi OS compatibility etc.
The only advantage really is using powershell commands/applications, so sometimes I will write my scripts in Powershell.
But in Linux I will also sometimes write my scripts in bash for the same reason, so it’s not really an advantage inherent to powershell, but the fact that it’s the “standard” of the OS.
I do like Powershell, but the problem with it is that it tried to be both a great scripting language and a great shell language, but the things that make it a good scripting language, such as verbose naming, objects, etc make it a worse shell language than shell languages designed to be shell languages first, and the things that it does to make it a useable shell language, makes it a worse scripting language than other scripting languages.
On average it may be the best language across all environments and use cases. The problem is that for any specific environment and use case it’s rarely one of the better languages.
To be honest, something like
$titled = gci *.md| ?{ (gc $_)[0] -like '# *' }
seems short and less error-prone than the bash equivalent. Not sure what it would be. Something like titled=(); for file in *.md; do if head -1 "${file}" | grep -sq '^# .*'; then titled+=("${file}"); fi; done
I think there's a lot of little gotchas in there, and not a little "API digging" for options, though it's simple in concept.Having to parse things is definitely a pain, at least until you get good at it. But the critical thing is that all the output is right there on the screen, and often in a format that's at least somewhat designed to be parsed. In PowerShell you have to go diving through the object hierarchy. That would be OK I guess if things were intuitive and the help and documentation were great but that's often not the case. And often the API semantics are designed for a different language altogether (C#) and the things you have to do to consume the API in PowerShell are ugly.
In my case:
- I had 10+ years of experience in linux/unix development
- Career change to AAA game development which is predominantly Windows based
- Day to day do Unreal C++ developing in Windows with Windows toolchains
- Still use WSL (and thus bash) for everything else, including DevOps related work, cloud/container development work, and personal notetaking/productivity via emacs+org-mode.
So, some of us have reasons to co-exist in multiple OS's. I tend to write up scripts in Python if I can so that there's at least a chance that I can run them in both Windows + Unix environments.
Maybe the IT department policies force them to. At least that's why I use Windows but do most of my development in an ssh terminal to a Linux system.
It’s “subjectively superior”.
If there’s one thing I can’t stand in IT, it’s people who think their personal preferences are equivalent to impartial facts.
You disagreeing with the author’s reasoning is evidence of just how subjective this topic is.
Architecturally speaking, Powershell (and other modern shells) are a huge improvement. But that’s only half the story.
Syntax wise, I find powershell a step backwards. It’s too verbose. Bash is too far the other way but if I’m using a REPL then I’d rather have something too terse than too verbose. Granted that’s just my preference but we are back to subjective arguments.
Ubiquity is another consideration. If you’re writing multiplatform bootstrapping code then Bash is a better option than Powershell. And we are back to subjective arguments again.
Broad terms are only true if you can agree on a specific context. But then they’re no longer broad and instead you’re relying on everyone agreeing to that narrow subjective definition.
Desktop applications would probably be the usual reason. It's all well and good if your application needs are covered by open source, but there's a fair bit of software with value add beyond open source that's never received a Linux version and WINE doesn't support well enough.
Of course, as you say, then you have the problem of the shortcomings of various *nix environments for Windows. MacOS can be a great solution... if your app still runs under MacOS (currently I'm trying to figure out how to get Fireworks to run under a minimal windows or mojave VM).
It's an idiosyncratic mix of ergonomics and habit probably. I don't really care since I get the job done efficiently (more so than a lot of "Windows natives" I observe).
I'm sure PowerShell and friends are great but it's enough of a shift to not be worth the learning curve if it's an environment you're just dipping into to do the bare minimum required to make something work.
That's quite poor design for a shell! It's like saying an OS is well designed except the mouse and keyboard input leaves a bit to be desired - because they were designed for scripting first and users second.
I'm a back-end developer/DevOps/Linux sysadmin who runs a small consultancy. I've been brought into a few Windows projects -- primarily on-prem/interactive installations. The past few occasions because the client was using Windows because the FE was being built using TouchDesigner. (Not sure it's still Windows-only but it was previously.)
So, it happens. Clients have many reasons for running Windows and depending on the project and its needs, I may accept the requirement or not. To use a recent couple of examples, if I can use GitBash, Docker, WSL and Python locally, then I'll probably take it on. If they're set on using PowerShell, C# and Azure, I'm probably out.
That all being said, if the project is interesting or high profile enough, I'll jump through whatever hoops are put in front of me.
I have to use PowerShell, but since it essentially wraps .NET services I very much prefer to either write C# (and have vastly more maintainable and testable code) or, when removing, invoke the APIs using a Unix scripting language. And these days I can write nice “portable” (ok, retargetable) binaries inside WSL and output a Windows build if I need to…
Git Bash is an elegant solution that brings some of the awesome Gnu utilities and does it on Windows terms and effortlessly. No shoehorn needed.
WSL is handy (those times I'm forced to use Windows - which is still better than when I'm forced to use Mac) but you have to remember the whole time you're in WSL, you're in Linux proper and all things work as Linux, generally, sort of. Git Bash is the power of Gnu right on Windows.
you use that too right? you better!
I don’t respect the attitude of doing whatever you’ve always done, even when working in a wildly different environment.
Imagine how Linux people would react if there were endless blog posts about using VB6 scripts on Linux by running them under WINE because “that’s what feels familiar.”
Sure, for a lark that’s hilarious, but not if you share your work with anyone else for any reason, even open source.
Heh, that would be hilarious. In fact someone should come up with a Linux distribution that uses NTFS, PowerShell, comes with .NET, WINE, and no coreutils and blog about it.
Alternatively, we could ask why the POSIX-sphere produces enough power users impassioned to, interested in and capable of enabling their pet projects on a closed and hostile platform for blog posts like the OP's but the reverse does not.
Parent is obviously choosing the "Veni Vidi Vici" side of the coin. If they can pull it off, why not ? It's their machine after all.
MSwindows is posix, why not?
windows supports tons of software that is not available on unix. People have to work with other people, spouses, university colleagues, whoever, maybe those people use MSWindows
An answer that I haven't seen offered yet: because Microsoft spend billions trying to gain and retain developer marketshare
this could serve as a wake up call to msys2 maintainers. every time i install it, i have absolutely no f ideia what the 6 variant icons mean and I couldn't care less since all of them mostly work... (thankfully I don't use windows much to reach a point the difference matters?) it's very overdue they just pick one and keep the others with better names in a compatibility folder or something. also, a better name.
- Paths look much better: C:/Users/doctorpangloss == /Users/doctorpangloss == C:\Users\doctorpangloss.
- The coreutils are just there.
- Works flawlessly as an SSH or Dockerfile shell.
- Fast and tiny. So small you can check it into git.
- The maintainer is very responsive and fixes bugs quickly, even in upstream.
git-bash has problems because of paths, terminal weirdness, and it doesn’t use native Windows APIs for a variety of things where it should. /c/ this, /mnt/ that. It’s not really possible to write multi platform scripts with it. With Busybox for Windows you can.
Most of the windows-based programmers I interact with don't know powershell any better than I do. They use GUI tools for interacting with git and the filesystem.
So for me, git bash for git and filesystem interaction is a superpower in these places.
I could learn powershell, and I'm pretty sure it's much better than bash for scripting, but I'm almost never really scripting in bash anyway, I use tools like F# for that. Powershell seems fine, but it's less well supported on Linux and I don't really need a shell based language.
So in the end, git bash keeps on working and I can focus my learning elsewhere. It's stable, still works the same way 15+ years later, so no need to change.
It’s basically a clr repl.
If you need to interact with windows/microsoft specific things (i.e. Active Directory), it’s pretty convenient
Always beem 'forced' to use Windows as my workstation by a couple of specialty apps without Linux alternatives that need direct hardware access. But have always used linux on everything else (servers, even my media centre). Because I'm sometimes a late adopter of new tech, until recently I used cygwin as my normal command prompt. There were few downsides to that if you just need access to unix tools, except package management is a bit annoying. Well, discovered the MS Terminal and WSL this year, and happily moved off cygwin. More recently decided to switch over from WS1 to WSL2 but still evaluating the move.
Now I have a unix "more native" environment AND a linux distro I'm familiar with. I've been accessing the Windows filesystem under /mnt for two decades so this is normal for me, and if filesystem access is slower, it's not a dealbreaker. All my existing scripts were trivial to update. The integration of WSL and VSCode is also pretty cool. It means my vs code environment environment is also working natively in my WSL distro.
Overall can understand some lingering negativity about MS efforts in this dept but after years of rejecting all things linux and open source, in general the 180 from the top is much appreciated and it has kept me on Windows a while longer...so mission accomplished microsoft. Just gotta do something about those pesky apps I still need...
Another wonderful benefit of git bash, is that because it is so commonly installed, if you want scripting for a dev/build tool, you can 99% of the time just use bash for your scripts, on linux and windows.
I use this all the time for my build scripts. build/x86_64-linux-gnu-gcc.sh, build/x86_64-windows-msvc-cl.sh, etc. (These are generally simple single command line unity builds, parameterized only by debug, opt, etc.)
Then from pwsh I can run these scripts with `& "$env:GIT_INSTALL_ROOT\bin\bash.exe" .\build\x86_64-windows-msvc-cl.sh`, similarly from cmd.exe. (Looks like a ton of typing, but it's not, I always Ctrl-R search and get a hit by just typing "git_").
No need to write a bat script, or even a pwsh one, pretty much ever again. Even if all you want is logic to cd to the script directory, parse a single script argument, check it's valid, provide a default, make some directories if not present, etc., bash beats all the alternatives on many fronts.
I'm not looking to have the same environment everywhere, I'm fine to have windows stuff optimized for windows on windows, and vice versa on Linux.
By that reasoning, I think clink is a much better option.
I'd just started working for a client that is fully entrenched in Windows, but we're doing bare-metal and Linux-y work. When I'd discovered I couldn't use Linux as my daily driver due to security/policy(/tooling) reasons, one of my co-workers told me about Git Bash, and it's worked out so well that ... <looks left, looks right> ... when we were given the opportunity to use Linux but VM Windows, I kept the Windows machine.
GitBash has made Windows painless for my work's use-case. I should probably throw a few $$ at them ....
[1]: https://scoop.sh/
Chocolatey, and to a greater extent, winget typically defer to the individual applications. In my experience they are more like glorified download indexes than actual repositories for managing software.
Installing something with scoop means downloading a declarative json manifest, with occasional, short pre-install/post-install levels of scripting (in all cases I've seen, it amounts to testing if a file is present, and copying it if so, etc.). The complexity that is tolerated for the scripting is much much lower.
I've also found chocolatey to be less well curated, with some odd packages with simple names that come from random people who don't seem to do a great job at maintaining the package.
scoop is preferable on every metric, IMO.
When I tried using winget, it mostly worked quite well, but occasionally would fail horribly. It'll happily detect and offer to upgrade programs that were installed outside winget, for example - great feature!! - except one time when I tried doing this it installed a new instance to a default location instead of upgrading in place, so now I had two installations. I don't trust it.
Scoop is simple and sensible and just works in exactly the way I expect it to. With the exception of needing to install scoop-search.
It doesn't occur to me to use it outside of that context. I use the command shell or Powershell depending on what I need to do.
WSL2 is great these days and can interact fine with Windows files (despite what this article says). But it's a lot more stuff, a full Linux install in a VM.
is that true when using Docker with WSL2?
Really? I always thought the subsystem worked translating OS calls back and forth without virtualizing an entire machine. It sure has an entire OS, kernel and all, but not a virtualization layer. Maybe that was the case for WSL but not WSL2?
WSL1 was a completely different approach, adding a Linux compatibility layer to Windows itself. There, you never had a Linux kernel running at all-- Linux syscalls would call into WSL, which would talk directly to the NT kernel to do whatever that syscall needed to do.
WSL1 didn't last very long (still present, but not actively being developed)-- turns out that reimplementing one operating system on top of another is a Hard Problem (see also: Wine). WSL2 avoids this entirely, and also avoids most of the impedance mismatches that you get when trying to reimplement POSIX on top of NT. WSL2 solved a whole bunch of compatibility problems essentially overnight that WSL1 never even got around to.
[1] https://learn.microsoft.com/en-us/archive/blogs/wsl/pico-pro... [2] https://devblogs.microsoft.com/commandline/announcing-wsl-2/
Microsoft realized trying to keep up with the ever-growing list of syscalls on Linux is actually not practical hence they opted for the virtualized approach with an actual Linux kernel to achieve 1:1 syscall compatibility.
They released that as WSL2. Technically the name is not accurate anymore as there aren’t any “subsystems” at work anymore.
Edit: I know I can use aliases and .bat files and whatever to make one look like the other, sort of, but then I learn some patchwork system and will be unable to use any other computer's login.
Although I can't get copy/paste to work in a remote text putty window. Sigh.
Depends on stuff like what terminal Putty emulates and so on, of course, but... Eh, have you tried using the old "MS/IBM CUA standard shortcut keys, [Ctrl]-[Ins] and [Shift]-[Ins]? (And, for completeness' sake, [Shift]-[Del] for Cut.)
AIUI, those are Putty's standard keys for that. I've always assumed that's because, you know, a 'Nix command line would interpret [Ctrl]-[C] rather differently.
1. I don’t understand how it relates to msys2 and where the boundary is, so I’m always reluctant to apply msys2 practices (documentation, advice, blog posts, etc.) to Git Bash.
2. I’ve been unable to figure out how to package scripts and other software for Git Bash so other users can install them.
I end up barely using it on my Windows 11 work machine because I'm allowed to have Linux VMs. Even with the VM overhead, and having fewer CPU cores and less RAM than the host, things still end up being way faster there.
I remember one time running a grep command on a large-ish (~ 1 GB) log file in Git Bash and waiting at least 5 minutes for it to complete. After getting impatient I did the same thing in a VM and it took about 30-45 seconds, at which point Git Bash still had not finished.
I mostly just use ubuntu under wsl2, though the file performance across the windows filesystem isn't the best.
I jokingly remark that most of my contacts with microsoft products are non consensual.