7 ms·
Linux Bash vs. Windows PowerShell
- remvee 6y ago@vedipen, thera's a typo in bash "if" example, an if-block is closed with "fi". edit: "if" example also misses a semicolon before "then".
- vedipen 6y agoactually then is supposed to be on the next line. updated it
- vehemenz 6y agoJust going to leave this here: Known Issues for PowerShell on Non-Windows Platforms https://docs.microsoft.com/en-us/powershell/scripting/whats-new/known-issues-ps6?view=powershell-7 https://docs.microsoft.com/en-us/powershell/scripting/whats-...
- goatinaboat 6y agoThe real difference is: in any reasonable bash script there will be a significant amount of awk, cut, tr and other commands to munge the output of one command into the input expected by the next. In PowerShell that totally goes away.
- mannykannot 6y agoBy 'goes away' do you mean you don't need it or there's nothing equivalent?
- jeremyjh 6y agoYou don't need it because the output of commands is a stream of objects rather than a stream of text.
- cowmix 6y agoIn the past 10 years I've noticed a lot of the manipulations I used to do in "awk, cut, tr" etc are baked into bash.
- IggleSniggle 6y agoI would love to see examples please!
- goatinaboat 6y agoEven so, there is still a lot of text manipulation that you need to do that is simply a distraction from the actual functionality of your script
- vedipen 6y agonicely said
- Koshkin 6y agoWell, the real power of PowerShell comes from .NET - there are very few built-ins.
- majkinetor 6y agoBetter stated like this: in Powershell you do business stuff 99% of the time, in bash you do it 1% of the time and arbitrary text parsing 99% of the time. To be more evil, your text parsing wont work on all platforms, and you probably didn't od it right or you need to be Reg Exp master which is something I rarely find even in senior devs.
- choward 6y ago> "sample1" -eq "sample2" -> True Why isn't this false? Is it doing type checking instead of a value comparison?
- Ididntdothis 6y agoThis must be a typo.
- vedipen 6y agoWas a typo. Corrected it :)
- skywhopper 6y agoThis is a pretty poor comparison of the two, especially once it gets into the syntax, of which most of the Bash examples are broken in some way. There’s also a lot of just straight errors, such as mentioning “true” and “false” “values” in Bash, which don’t exist—the userland toolset includes “true” and “false” programs that can be used to inject desired exit code status into some logic flow, but there is no real concept of a Boolean type. Overall, if you’re going to provide a rundown of how x and y compare, you should probably give real examples, not just a laundry list of “instead of grep, we use select-string; instead of sed we use -replace”. But how do you use them? I worked with Powershell some over a decade ago, but this article doesn’t give enough info for me to understand what to do with the information it’s providing. And given the errors in describing how Bash works, I’m skeptical that the Powershell examples are syntactically correct either.
- oefrha 6y agoTo be pedantic: true and false are shell builtins in Bash and most other modern *nix shells. They are commands in the POSIX spec, though.
- majkinetor 6y agoAgree. I have just seen scroll size and imediatelly closed it.
- oefrha 6y agoI think PowerShell doesn’t really compete with Bash, it competes with Python, Perl or the like. And I haven’t found it to be superior in that aspect other than at integration with Windows system stuff.
- tcbawo 6y agoPowerShell should raise the bar for lowest common denominator of scripting on Windows systems. I've dabbled a bit it, but I was disappointed it was so tightly coupled (possibly by necessity?) with the .NET runtime version. This makes it much more difficult to update on old systems. I had to write PowerShell in the oldest compatible version. It's almost like dealing with JavaScript implementations of all versions if IE. Other scripting langues like Python provides user-space virtual environments with forward/backward compatibility. Does PowerShell have a universally available package manager (ie. Windows Update)? I don't spend much time in the Windows world. But, the friction of using PowerShell was higher than my tolerance.
- majkinetor 6y ago> Does PowerShell have a universally available package manager It does, called OneGet and recently rewritten. You can also use chocolatey. For windows update: https://www.powershellgallery.com/packages/PSWindowsUpdate/2.0.0.4 https://www.powershellgallery.com/packages/PSWindowsUpdate/2...
- lostmsu 6y agoPowerShell Core should solve that problem, as you can ship it with its own .NET Core runtime.
- majkinetor 6y agoNo it doesn't. It is "shell language" designed for shell usage, not general purpose language, like Python.
- AdmiralAsshat 6y agoArticle might be more successful if it demonstrated some of the advantages of PowerShell, such as returning the output as objects instead of lines, and what you can do with them as a result. I'll admit to being in the "Bash is what I know, I'll just install WSL/Cygwin/PuTTY on my Windows workstation and be done with it" crowd. PowerShell is on my bucket list of things I need to learn at some point, or at the very least just get a RosettaCode cheatsheet of common stuff I do in bash and how to do it in PowerShell for when I'm stuck on remote Windows servers.
- rasengan 6y agoI made a Bash script for HN, and while I’m the furthest thing from an expert in Bash (I’m a newb), I was impressed by a contribution made with a port of the script to Powershell[1]. You can see the same script compared in Bash, Powershell and Python! Prior to this I had never really explored PS, but it is definitely cool Shameless Plug: [1] https://github.com/realrasengan/autobahn https://github.com/realrasengan/autobahn
- GordonS 6y agoThanks for posting this, it's interesting to see the same problem solved in 3 languages. I use Bash for all my scripting needs, on both Windows and Linux - I just can't get over my dislike of Powershell's verbose syntax and Pascal-Snake-Case naming! But some things are difficult or finicky in Bash; maybe I should stop putting off learning Python...
- gabrielsroka 6y agoPowerShell commands are not case sensitive. Commands use <Verb>-<Adjective><Noun> structure, for example Get-ADUser gets an AD user. Once you learn that, it's easy to know that the equivalent command for AzureAD is Get-AzureADUser. It did take me a while to get used to it, but it does make sense, and it makes it easier to learn/remember commands.
- GordonS 6y ago
- BiteCode_dev 6y agoIf you need crossplatform scripting and will install a runtime, why go powershell or bash ? Something like ruby or python seems more adpated.
- vedipen 6y agoPowershell is more for administrative automation
- BiteCode_dev 6y agoSame, unless you are on windows only, ps has no benefit. The selling point of ps is the piping of objects. Mac and linux api don't output those.
- death-by-ppt 6y agoPowerShell is built on top of .Net Core, which is also cross-platform. It does pipe objects on all 3 platforms.
- BiteCode_dev 6y agoNot the point. No unix api does.
- majkinetor 6y agoIts designed for shell usage, python, ruby and friends don't feel "native" in that manner and are simply akward for many cases by missing most improtant thing - pipeline. People use them because bash suck and because they want to be full stack. Besides, Powershell comes with OS.
- BiteCode_dev 6y agoKeyword is "crossplatform"
- riobard 6y agoNot sure what the website did, but the page does not even scroll on iPad Safari.
- uk_programmer 6y agoThe article does need a lot more fleshing out. As many have said here the thing that powershell has over bash is piping objects.
- fargle 6y agoThat presumes, which is always my complaint about this subject, that this is an advantage. I'd say that's what sh, ksh, and bash have over powershell (tm). The problem with pipelines of objects is that it is inherently a more strongly typed, defined, interface. And therefore special-case. Streams of characters is as loosely typed as it gets. And for processing text files and gluing disjointed tools together, it may seem crude, but is very effective. Especially for quick text processing jobs, or automating series of commands. The UNIX shell concept has been honed and refined over 50 years to do exactly what it does, and it does it very well. It is also extremely generic. This choice was on purpose and has stood up well to the test of time. Powershell is somewhat a perl-for-windows-admins. A write only pile of special tools and special cases for the peculiarly baroque system interfaces of that particular OS. Neither better or worse than VBA from what I can tell, just the passing style this decade.
- uk_programmer 6y ago>The problem with pipelines of objects is that it is inherently a more strongly typed, defined, interface. And therefore special-case. Not true. While I am somewhat of a novice at powershell, I didn't find that to be the case at all. > Streams of characters is as loosely typed as it gets. And for processing text files and gluing disjointed tools together, it may seem crude, but is very effective. Especially for quick text processing jobs, or automating series of commands. Well done for the Pavlovian regurgitation of how piping text output is supposed to work. I don't like working with it. It is okay for small scripts. However if it is mildly complicated I would use something like Python (not powershell). Powershell is great for small scripts where you need to do sysadmin tasks e.g. I have a script where I setup my SQL Database aliases for development. Piping text about doesn't help me in these sorts of tasks. I need to go "I need to find where the real database is, and then map those properties to an alias". That works better with piping objects. It works kinda like functional concepts such as map(), reduce() etc. You should use the right tool for the right job. > The UNIX shell concept has been honed and refined over 50 years to do exactly what it does, and it does it very well. It is also extremely generic. This choice was on purpose and has stood up well to the test of time. You know not everyone thinks that Unix was the pinnacle of human achievement.
- JamesSwift 6y agoI dev on a Mac and do a lot of 'quality of life' scripting with Bash. I recently had to do something on a PC and figured I'd try out Powershell. I have to say, it was a joy. Dealing with objects instead of text is so refreshing, it really is a game changer especially when you start integrating with COM (e.g. excel). The only things I found worse: 1 - error handling doesnt bubble in the same way. e.g. I can't do an equivalent of `set -e`, or do `cmd && other_cmd` 2 - return values are very nuanced. If you don't understand how function statement contribute to the result, you will spend a lot of time debugging odd issues
- syrrim 6y agoThey added && recently, apparently.
- amarraja 6y ago$ErrorActionPreference = "Stop" At the top of the script is the closest I fount to `set -e`
- noinsight 6y agoThe equivalent to `set -e` is setting the `ErrorActionPreference` variable to "Stop" - all errors become terminating.
- mpawelski 6y agoAs people mentioned there is `$ErrorActionPreference = "Stop"` but it's not equivalent to bash `set -e`. Looks like there is no consensus if we'll get something similar in the near future (https://github.com/PowerShell/PowerShell-RFC/pull/88 https://github.com/PowerShell/PowerShell-RFC/pull/88)
- ape4 6y agoI hate how long the PowerShell comand names are. Yes they are sensible but arg. Eg grep vs Select-String as the article mentions.
- majkinetor 6y agoSeems like arbitrary comment as you can have all forms in powershell.
- chokolad 6y agoSelect-String is alisased to sls and if you are writing a one-liner or using it from shell - just use the alias. It is recommended that you use full names in scripts for readability reasons so most script examples on the web have those long obnoxious names. Also, tab completion works really well with command argument names so in practice it's not that many keystrokes.
- metreo 6y agoHow is the user experience different in Win where you actually need to use PS compared to the users need for Bash on Linux?
- weibing 6y agoa useless article
- vardump 6y agoThe biggest issue in PowerShell is very slow script execution startup time, 300-500ms seems to be typical. So forget about externally launching PowerShell scripts thousands of times. The second biggest issue I've seen is generic lack of robustness and maturity. Default cmdlets do the common case nicely, but once you have something unusual in the mix things go wrong fast... By unusual, I mean things like filesystem junctions, volumes mounted into directories. If you want colors, Write-Host is nice. Except that it goes only to console AND to (undocumented?) file id 6! (same way like stdout goes to 1, stderr to 2, etc.). PowerShell seems to behave slightly differently in each Windows version. A script that's written for Win10 1803 might just break under 1809. I don't have much issues with Bash etc., even sh. It performs well and is very robust. I do hate its many footguns, like how easy it is to get escaping wrong.
- JdeBP 6y agoIt very much is documented. Get-Help about_Redirection Received wisdom about Write-Host, by the way, is that one rarely wants it.
- vardump 6y ago> Received wisdom about Write-Host, by the way, is that one rarely wants it. Yeah, totally got that. But how else you can generate colorized easy-to-read output, where yellow warnings stand out? There should be something like Write-Host that outputs colors to console stdout handle and silently drops color properties when writing to (anonymous) pipe stdout handle. Color escape codes could be even better.
- colonwqbang 6y agoThere are so many errors in his bash code, it makes you doubt the author's basic bash competence. None of his examples use fully correct bash syntax.
- mpawelski 6y agoI use PowerShell for quite some time (mostly windows/.NET guy here) and it would be great tool except one thing: the amount of not obvious quirks is astounding and not really decreased over the years. Probably because of avoidance of breaking changes, which is understandable where there are tons on powershell scripts out there. But for for someone who uses it mostly as a shell to write one liners or occasionally write a script not longer than 30 lines of code, it's really discouraging when you encounter again some unexpected behavior (even though you thought you learned powershell "good enoguht" couple years ago).
- majkinetor 6y agoYes, this could be problematic. But any language has this kind of quirks (rust, ruby etc.). The problem is not like with C where you have quirks forever. Once you learn it, you know about them.
- elagost 6y agoHaving used both, in a personal and professional capacity, I don't think they occupy the same "real estate". If you have a bunch of Windows systems to admin, sure, PowerShell works great with all the Windows built-ins. In fact, it works with SCCM pretty well. If your systems are Linux-based, you can use shell scripts to interface with something like Chef or Rundeck. Use something that works natively and is designed for the platform you need to orchestrate.
- altmind 6y agoHave anyone replaced *sh with powershell on linux for scripting?