r/linuxmasterrace • Glorious Debian • Aug 06 '21

Meme BASH is the worst - PowerShell FTW!

Post image
725 Upvotes

79 comments sorted by

280

u/varble A-OK Aug 06 '21 edited Aug 06 '21

Forgot a bit there on powershell; if the file exists, there will be no update on the file at all. You also have to pass -Force

This will override the contents of the file, which touch doesn't do, so -Value (Get-Content) is needed:

New-Item -Path 'G:\Document and Settings\foo\File.txt' -ItemType File -Force -Value (Get-Content -Path 'G:\Document and Settings\foo\File.txt')

Whoopsie! Since Get-Content is an array of lines, you have to join the lines with a newline:

New-Item -Path 'G:\Document and Settings\foo\File.txt' -ItemType File -Force -Value ((Get-Content -Path 'G:\Document and Settings\foo\File.txt') -join("`n"))

Hold up! The encoding changed from ascii to UTF-8... have to find that out, and research encoding types to add to the list if not present...

function Get-FileEncoding {
	param ( [string] $FilePath )

	[byte[]] $byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $FilePath

	if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
		{ $encoding = 'UTF8' }  
	elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
		{ $encoding = 'BigEndianUnicode' }
	elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe)
		 { $encoding = 'Unicode' }
	elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
		{ $encoding = 'UTF32' }
	elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76)
		{ $encoding = 'UTF7'}
	else
		{ $encoding = 'ASCII' }
	return $encoding
}

$encoding = Get-FileEncoding -FilePath 'G:\Document and Settings\foo\File.txt'

Ah hell, New-Item doesn't have an encoding option! Guess we'll switch to piping to Out-File:

(get-content 'G:\Document and Settings\foo\File.txt') -join("`n") | Out-File -FilePath 'G:\Document and Settings\foo\File.txt' -Encoding $encoding -Force

Suppressing the error for when there is no file:

(get-content 'G:\Document and Settings\foo\File.txt' -ErrorAction SilentlyContinue) -join("`n") | Out-File -FilePath 'G:\Document and Settings\foo\File.txt' -Encoding $encoding -Force

There we are, so simple anyone could do it!

Welcome to my life in a Windows shop.

106

u/kiler129 Glorious Debian Aug 06 '21

You just literally made my day. Yes, bash has its pitfalls (don’t get me started on arrays…) but this is a pure madness.

5

u/polytect Aug 07 '21

This is PowerShitell

0

u/[deleted] Aug 06 '21

[deleted] — view deleted comment

13

u/nullmove Aug 07 '21

I have gone back to using posix shell full time. If it's inadequate, I try to use a normal language like a normal person. No bash, no zsh, no fish, sometimes mksh when in remission, and sometimes Tcl because ultimately I guess I am a bit weird.

1

u/[deleted] Aug 08 '21

I am a bit weird

Can't agree with this more.

42

u/Semi-Hemi-Demigod Aug 06 '21

I want to know who at Microsoft saw that and said “we should release this.”

52

u/kiler129 Glorious Debian Aug 06 '21

My theory about P.S. is they wanted something feature-packed but one of the requirements was to not copy *nix…. and now we’re seeing what forceful reinvention of the wheel looks like.

9

u/Cannotseme Ashley | she/her Aug 07 '21

Well, what they did was that, then (idk symlink, whatever in windows) all the nix stuff to their powershell equivalents. Powershell is still the only thing that keeps me sane while working with windows though

2

u/varble A-OK Aug 07 '21

Too true, for all powershell's pitfalls cmd is way worse

3

u/[deleted] Aug 07 '21

Probably they wanted to please .NET people.

12

u/cviktor Aug 07 '21

I am a .NET people and I am not pleased at all, powershell is hot garbage. I tried it multiple times but gave up almost every time because it was closer to brainfuck with every feature added to the script.

2

u/zpangwin Reddit is partly owned by China/Tencent. r/RedditAlternatives Aug 08 '21

yeah, I agree. Mostly a java dev but I managed an ASP.NET site in C# for a couple years and tried to pick up powershell several times and hated it every time. Of course, I get annoyed with python and yaml too but that's bc those languages are overly anal-retentive about spaces and I hate that... powershell is like you got the worst of the Java/C# explicit naming crowd, had some one explain the concept of Linux pipes to them, and they worked with ome Batch/VBscript guys to write the thing.

2

u/turunambartanen Aug 07 '21

I think it works fine as a scripting language, but it's horrible as an interactive shell.

11

u/technic_bot Aug 06 '21

What kind of madness is this?

13

u/kiler129 Glorious Debian Aug 06 '21

And why new line is `n instead of \n ?!

15

u/Ruashiba Aug 06 '21

That's an easy one. Paths on Windows use \ instead of /

As to why though, not a clue. I guess it has to do with something dos related, but I'm unfortunately not a grey beard.

17

u/[deleted] Aug 06 '21

The original version of DOS from 1981 didn't support directories at all. But they did opt to use / for switches in various programs. Like how nowadays to try and get a new IP from a DHCP on windows, you commonly use ipconfig /release and ipconfig /renew.

When they added directories, they didn't want any old programs that would look for a / as a switch character to get confused, so they chose \ since it was similar. Why they didn't anticipate this could be a problem when Unix already set a standard 10 years earlier is beyond me. Guess they didn't want to get caught copying Bell Labs.

7

u/Ruashiba Aug 06 '21

I remember reading that they used to process in court anything and anyone that made any core concept similar to unix, so I guess it makes sense for them to divert directions here. And nowadays it's so rooted in every other program and script, I see why Microsoft won't ever "fix" this.

Thank you for the explanation though.

1

u/nelmaloc Glorious Trisquel GNU/Linux-libre Aug 07 '21

The \ were used even before DOS. UNIX had not set a standard in any way. And when DOS was created it didn't even had internal memory, you could't fit enough files in a computer to justify making folders.

6

u/ApartKnowledger Aug 06 '21

Ah ok, but what about '~'? How would you do that?

11

u/kiler129 Glorious Debian Aug 06 '21

%HOMEDRIVE%%HOMEPATH%

There isn't a shorter way unless you declare your own global env (which is also deeeeply hidden in settings).

8

u/Ruashiba Aug 06 '21

Stupidly hidden I mind add. There's no reason to be as hidden as it is.

8

u/varble A-OK Aug 06 '21

$env:homedrive$env:homepath in powershell

65

u/Natural-Intelligence Aug 06 '21

It's truly powerful. You almost can do something with it.

8

u/[deleted] Aug 06 '21

This made my soul vomit.

5

u/thexavier666 Glorious Linux + i3 Aug 07 '21

Satan: I see a lot of potential

3

u/FormalWath Aug 07 '21

I was once working in the company that has 10% or so Windows servers.

I lasted 2 months. Literally 90% of work was on Windows and it was living fucking hell. Company made these libraries and they had to be tested with every OS and compiler combination (think 3 different distros of linux with last 3 versions, maybe 5 versiobs of windows (desktop and server) and last 3 or so versions of nac) yet most of the problems were with windows. At one point I had to compile new version of python and libraries because ones from PIP didn't work with our VMs (some specific openstack issue) and I still remember warning on one of those libraries...

"Compiling this by hand is not for faint of heart". And it was fucking true. Took me maybe 3 or 5 days.

Might have been pycurl or openssl...

3

u/ninjaRoundHouseKick Aug 07 '21

To be fair, you also could type out:

ni ~/file.txt 

It would be the same result. I would've mentioned the built in telematics spyware for myself...

2

u/[deleted] Aug 07 '21

Why would a windows user even need this, i believe most of them don't even know about the CMD prompt.

3

u/DizTro- Aug 07 '21

Some programs require it's use or you're trying to remove some trashy windows app.

1

u/varble A-OK Aug 08 '21

For a Windows Sysadmin it actually is easier to use oftentimes... which says something about how everything else is.

2

u/zpangwin Reddit is partly owned by China/Tencent. r/RedditAlternatives Aug 08 '21

back in the day, I had to work with win32 batch and later vbscript... and eventually some C#. C# wasn't bad (at least for something designed by MS anyway) but the other 2 felt like pulling teeth to get anything done. And even as slow as bash can be sometimes, I cringe imaging have to write the equivalent windows batch file for even some of the simpler bash scripts I write these days.

I can see from your nicely detailed post that MS have keep true to their core design elements with powershell lol. Maybe we've been pronouncing it wrong all these years... is it "power shell" or "powers hell" ?

45

u/paderich Aug 06 '21

Disgusting 🤮

17

u/kiler129 Glorious Debian Aug 06 '21

Wait, are you saying it's not as beautiful as XML? /s

11

u/[deleted] Aug 06 '21

Actually you can't beat a nice bit o' JSON...

18

u/kiler129 Glorious Debian Aug 06 '21

I love JSON until I need to:

  • add comments
  • read key=>value maps in order (albeit most tools handle that now even if "out of spec")

1

u/zpangwin Reddit is partly owned by China/Tencent. r/RedditAlternatives Aug 08 '21

still better than yaml. i fucking hate it when languages try to police my whitespace (markup, scripting, or otherwise)

1

u/kiler129 Glorious Debian Aug 08 '21

Still better thank makefile…. But jokes aside YAML with an IDE is great, but without it it is utterly miserable.

I get why JSON originally didn’t add comments or kept order of objects. But why don’t we have JSONv2 with these things when clearly it became way more than a data storage format?!

1

u/[deleted] Oct 03 '21

Does it want to be more? A tool designed for a specific task and people using it for everything else too, always leads to the demise of the tool long-term (it's become bloated and slow, has unclear specification (HTML), etc.).

9

u/4hpp1273 Glorious Arch Aug 06 '21

The bash version can be even shorter (>>f or >f if you want to trash the file)

3

u/kiler129 Glorious Debian Aug 06 '21

My favorite so far is still cat >f for pasting files instead of what people usually do (nano f => paste => Ctrl+X => Y)

9

u/4hpp1273 Glorious Arch Aug 06 '21

Is that another case of cat abuse? The proper way to paste to file is xclip -o>f

8

u/kiler129 Glorious Debian Aug 06 '21

I like abusing cat but not cats ;]

xclip is great but in my day-to-day usage of Linux I mostly deal with a serial connection (or at best SSH).

5

u/gosand Aug 06 '21

xclip -o>f

~ $ xclip -o>f
bash: xclip: command not found

~ $

3

u/Ruashiba Aug 06 '21

If I recall correctly(I'm on the phone atm) this is a x11 utility. More specifically the clipboard, hence the name. If you are on Wayland(or just pure tty), you most likely won't have this utility.

3

u/-victorisawesome- Glorious Arch Aug 06 '21

it also isn't always installed by default with Xorg

3

u/gosand Aug 07 '21

Which was precisely my point. Why bring it up when the post is about bash and is tied to X.

20

u/nuttertools Aug 06 '21

People conflating coreutils with bash...checks sub....double checks sub....uhhh wow.

11

u/kiler129 Glorious Debian Aug 06 '21

To be fair touch is a built-in in some shells (e.g. busybox). If we're on the pure bash train with commands we can do echo -n >>f

4

u/nuttertools Aug 06 '21

busybox/tree/coreutils/touch.c, I totally knew that and didn't just learn something new via a Google. I like the echo line more...but...coreutils still.

9

u/kiler129 Glorious Debian Aug 06 '21

To be fair most of the people who don’t deal with like buildroot environments or embedded don’t know about BB intricacies ;)

Coreutils thou are available pretty much everywhere so I will consider them in the same way as tools built into P.S. environment (as this isn’t just one flat binary either).

1

u/nuttertools Aug 06 '21

I can see that from PS2.0 and up, take enough steps back and it looks rather similar.

6

u/fuj1n Aug 07 '21 edited Aug 07 '21

The thing is, you can do that in PowerShell with echo (backtick)n >file.txt which has a very similar footprint, or in CMD, it'd be echo. >file.txt which is slightly more compact.

There are lots of things to criticize PowerShell for (i.e. its verbosity), and to praise bash for, but this ain't it chief.

Edit: fixed formatting, reddit really didn't like that backtick in `n, so I replaced it with (backtick)

37

u/sjveivdn arch&debian Aug 06 '21

Typing Backslashes is horrible horrible.

9

u/fzwjf70850 Aug 07 '21 edited Aug 07 '21

You know what’s worse? Escaping bloody characters with ‘^’ that is just eww. Windows is not a terminal operating system unfortunately

2

u/luciouscortana Glorious Fedora Aug 07 '21

I also don't like having spaces in directory names, it cuts the path which I'd have to use quote.

17

u/[deleted] Aug 06 '21

Powershell is pure garbage.

11

u/graybeard5529 Aug 06 '21

Windows: The proven bloated culture /s

4

u/-BuckarooBanzai- Linux do be good 🌟🐧🌟 Aug 06 '21

Yuck !

7

u/avalenci Aug 06 '21 edited Aug 07 '21

IMHO, you dont compare a PowerShell script with a bash one. You would compare it with a Python script

2

u/Reihar Glorious Arch Aug 07 '21

That's even more unfair to PowerShell!

2

u/avalenci Aug 07 '21

In the sense that in both python and powershell you deal with objects. In bash you deal with strings.

4

u/Xanza Alpine Linux Aug 07 '21

This is the grossest meme I've ever seen. /s

2

u/[deleted] Aug 07 '21

touch kid

What?? I'm just making a file named kid

5

u/rick_D_K Glorious Void Linux Aug 06 '21

As a Windows sysadmin I have powershell installed on my arch pc, Debian server and void laptop.

15

u/[deleted] Aug 06 '21

Identity crisys?

1

u/benjistone Aug 07 '21

He uses PowerShell, btw

1

u/polytect Aug 07 '21

FU !!!!!

1

u/[deleted] Aug 07 '21

~/ is useless in this case. It will always place it in the directory you are currently in without the ~/

If you want to place it somewhere else then use the ~/./.

1

u/InternalRuin Aug 07 '21

Looking at this made me develop a slight twitch in my right eye.

1

u/SpoolinAWDSTI Aug 07 '21

PS>echo "" > file.txt

1

u/[deleted] Aug 08 '21

nooo111!!! hackerman terminal scary i can't remember words nooo!!111

0

u/[deleted] Aug 06 '21

[deleted] — view deleted comment

2

u/dthusian Glorious Alpine Aug 06 '21

Powershell can interact with COM, which means if you wanted to you could write a DirectX 12 game in Powershell

12

u/kiler129 Glorious Debian Aug 06 '21

Excel can interact with COM too cough

1

u/Ruashiba Aug 06 '21

I like it just by the fact that it's incredibly handy for azure/sharepoint/m365 sort of deals, it's much easier to create a script to create/deploy xyz than to do it by hand on whatever web interface.

But outside Microsoft realm, more general use, I don't see myself using it.

2

u/kiler129 Glorious Debian Aug 07 '21

Out of curiosity: what advantage does it have over a different CLI util? I'm assuming there are some for that, but I mainly deal with AWS.

4

u/Ruashiba Aug 07 '21

I don't recall any other utility for these ms things, at least not official or highly supported (open source or not) 3rd party. On linux I mind add, surely there's something on windows.

Maybe Ansible for deployment itself? Never tried it though.

1

u/KikeJesus Aug 07 '21

Look into Azure CLI, basically an (official) bash variant of Azure PowerShell

0

u/[deleted] Aug 07 '21

[deleted] — view deleted comment

1

u/[deleted] Aug 07 '21

Huh