DEV Community

Cover image for *nix Bash shell vs Windows PowerShell: Two paradigms
Hamza Tamenaoul
Hamza Tamenaoul

Posted on • Edited on

*nix Bash shell vs Windows PowerShell: Two paradigms

Originally posted in my portfolio.

PowerShell and Bash Shell, have quite a lot of differences. The first one is a complete scripting environment, the second is only a command line interpreter. One is suited for the management of windows servers, the second is more suited for development environment. These differences might look obvious when discovering them for the first time. But what really held my attention, was the fact that PowerShell use Objects, when Bash Shell uses plain text.

Let's take a simple linux command.

$ ls -l

The output of this command will be returned in a plain text format, that input will can be for example piped to another command like grep

$ ls -l | grep Desktop

In this example the bash would take the output of the ls -l command a string parse it looking for the pattern set in the parameter.

Windows PowerShell behaves differently. Let's try to repeat the same example in it.

PS > dir

Now, if we want to look for a specific pattern on the text, we will pipe the result to the Select-String command.

PS > dir | Select-String -pattern "Desktop"

At first glance you will notice a difference in how the output is displayed. Unlike grep which displayes the line where it found the pattern with the pattern highlighted. But it's not simply a display style or choice. The output is the object that matches the pattern.

To see this behaviour in action let's assume that for the linux command the output is:

drwxrwxrwx 1 hamza hamza    4096 Nov  4 18:29 Desktop
drwxrwxrwx 1 hamza hamza    4096 Nov  4 17:15 Documents
drwxrwxrwx 1 hamza hamza    4096 Nov  4 17:15 Downloads    
drwxrwxrwx 1 hamza hamza    4096 Nov  4 17:15 Favorites
drwxrwxrwx 1 hamza hamza    4096 Nov  7 10:56 IntelGraphicsProfiles
drwxrwxrwx 1 hamza hamza    4096 Nov  4 17:15 Links
drwxrwxrwx 1 hamza hamza    4096 Sep 22 01:26 MicrosoftEdgeBackups
drwxrwxrwx 1 hamza hamza    4096 Nov  4 17:15 Music
drwxrwxrwx 1 hamza hamza    4096 Oct 20 12:26 OneDrive

Then if we use:

$ ls -l | grep drw

We will get the same output but this time with drw highlighted. But for the first PowerShell command if we get:

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       04/11/2018     17:15                Contacts
d-r---       04/11/2018     18:29                Desktop
d-r---       04/11/2018     17:15                Documents
d-r---       04/11/2018     17:15                Downloads

and do a search on this output using:

PS > dir | Select-String -pattern "d-r"

We will get nothing in return. Because the column Mode column is a non searchable parameter by Select-String of the output Object of dir (the alias of Get-ChildItem).

If you call Select-String with wrong arguments, it will send an error message for each file object it iterates throw.

Learning about the differences in different technologies is very interesting, a way to discover how different people give different solutions to the same problems. By understanding the power of each technology and it's short falls, you will start to see technology in a whole new way.

Top comments (0)