r/PowerShell • • Nov 28 '21

Bash vs PowerShell Cheat Sheet

https://blog.ironmansoftware.com/daily-powershell/bash-powershell-cheatsheet/
142 Upvotes

17 comments sorted by

16

u/wtgreen Nov 28 '21

Write-host is not the equivalent of echo... write-output is.

2

u/[deleted] Nov 29 '21

Good point! Updated.

5

u/[deleted] Nov 28 '21

This is not how you concatenate strings in bash. Also, you don't use double quotes on the variables during echo, which is advised.

And how come you're using normal intendation on the powershell code, but potato-intendation on the bash code? Any particular reason?

And why do you create the "make directory" in bash inside of a subshell, but in the powershell code you create it in the same process?

I mean, your article is a quick and dirty starting point, sure. Better than nothing.

But for publication to the rest of the world it's apparent that you have to polish it... a lot.

2

u/BlackV Nov 28 '21

you talking about

`mkdir $newdir`

it think the back ticks were editor leftovers

2

u/[deleted] Nov 28 '21

I don't think so.

2

u/BlackV Nov 28 '21

So what did you mean by new sub shell/same process then?

0

u/[deleted] Nov 28 '21

I meant a subshell. Like you understood it correctly the first time.

4

u/[deleted] Nov 28 '21

Noice!

4

u/[deleted] Nov 28 '21

Those are terrible examples of While Loops. Rather than use break to get out of the loop, you should be setting the sentinel to a value that stops the loop at the while () condition test. In the case of the bash loop, you should be setting $valid to false so it stops on the next condition check, and the Powershell version is a infinite while loop, which are usually considered a special subset of While Loops.

3

u/fuzzylumpkinsbc Nov 28 '21

Looks good, I'll save it to do the reverse, kinda wanna learn some basic bash

2

u/jsiii2010 Nov 29 '21

I prefer "add-content" over ">>" for appending to a file. >> appends in utf16 format, and doesn't check what the current format is, so it can mix utf8 and utf16 in the same file.

1

u/MonkeyNin Mar 22 '22

Yep. Using >> in Windows Powershell doesn't care what the file's encoding is

Using >> In powershell defaults to utf8

This config will help mitigate most of the crazy encoding defaults in windows powershell.

1

u/toast-gear Nov 28 '21

Why is the powershell indented but bash isn't, no one writes bash scripts like that.

13

u/[deleted] Nov 28 '21

[removed] — view removed comment

6

u/sotonohito Nov 28 '21

Eh, while I'm not fully on board with the more verbose structure of powershell autocomplete takes a lot of the sting out.

0

u/[deleted] Nov 28 '21

[deleted]

1

u/BlackV Nov 28 '21

useful