Linux Bash vs. Windows PowerShell

栏目: IT技术 · 发布时间: 4年前

内容简介:With the addition of Linux subsystem in Windows and PowerShell having native support in Azure and Windows devices, having knowledge about both the scripting languages has become more and more important.Personally, I used to be a diehard zsh user, but the m

With the addition of Linux subsystem in Windows and PowerShell having native support in Azure and Windows devices, having knowledge about both the scripting languages has become more and more important.

Personally, I used to be a diehard zsh user, but the most popular shell on Linux and Mac is ofcourse the Bash. I meet so many people day to day, who stay afraid of PowerShell and always find ways of running a bash on Windows wherever possible. This shouldn’t be the case.

Bash

Bash allows you to type in commands, and to execute those commands, and then see the results being displayed on screen, and it also allows you to take information from one application and pipe it or pass it into the next application, pipe it into the next application and so on. That information is passed as text , and it allows you to take the output of one application and pipe it into the next.

CMD to PowerShell

In Windows, we have the Cmd shell which many people still think today is MS DOS. It isn’t actually MS DOS, but it was originally built to be compatible with MS DOS. It supports many of the same commands, and much of the same syntax. In Cmd you can also execute commands, you can also pass information from one command to the next, and that information is passed as text . However, Cmd is relatively limited in today’s world. Microsoft introduced a new technology back in 2005, called PowerShell .

PowerShell is a whole new approach to command-line shell technology. It essentially still provides you a command-line syntax, and scripting syntax that you can type commands into but it takes the information that you can pass from one application to the next, and passes it as rich objects that you can query and manipulate far more easily than you can process text.

PowerShell exposes much of the WMI, COM and .NET object model in an interactive and scriptable command line environment - and that this command line environment continues to borrow many of the concepts - such as pipelines and I/O redirection - from Unix shells or Bash. Since PowerShell uses these objects, you can do things like create HTML and possibly Excel spreadsheets on the fly if you knew enough.

PowerShell Core

PowerShell was originally designed to be a management tool for Windows. But now PowerShell is available for Linux and Mac too, as PowerShell Core.

PowerShell Core is an attempt to make PowerShell multiplatform. Though, there always would be some modules which would be available only on Windows. Most of them related to Hyper-V, which does not run in Linux and Mac.

After 3-4 years of using bash, it took me almost 6 months to stop typing bash syntax to PowerShell command line. Here is me sharing some key differences. Though bash is available in Windows now though the Linux subsystem, Bash on Windows comes with less than 40 internal functions and around 100 helper programs.

Syntax

I would recommend learning PowerShell, instead of just sticking with Bash. Users familiar with the tool deploy, manage and repair hundreds of systems from any remote location, automate runbooks and use C#-based PowerShell script files to automate repetitive tasks. It is really powerful.

In PowerShell 6, most of the Bash commands work, though there are some basic syntax to PowerShell.

Most frequent being the $ symbol, used for denoting the variables.

VAR = "Hello world"

works in Bash but doesn’t work in PowerShell. In PowerShell, all variables should start with $ .

$VAR = "Hello world"

works in PowerShell.

The comparision operators. To compare two strings, == and != work in Bash,

while in PowerShell, we have -eq and -ne .

"sample1" -eq "sample2" -> True

We have sed in Bash to transform a string, in PowerShell we have -replace .

The popular Bash command grep doesn’t work in PowerShell. We use select-string instead.

In Bash, we use boolean values as true and false but in PowerShell we are used just like variables, $true and $false .

The foreach loop in Bash is

for var in $array do
    codeblock
done

In PowerShell, its

foreach ($var in $array)
{
    codeblock
}

And the most frequently used ‘if’ loop, in Bash,

if [ condition ] then
    codeblock
if

while, in PowerShell, just like we write in C#,

if (condition)
{
    codeblock
}

The last interesting difference I would state is the path variable.

In Bash, $PATH searches the path while in PowerShell we have $env:path

These are the few key differences which I always keep on the top of my hand while working with PowerShell. Hope these tricks help you get more familiar with PowerShell.

Resources

  • Learn Bash
  • The best way to ramp up on PowerShell is to follow the official documentation (also open-source).
Non-programmers: 1, 2, 3, 4, 5 ..  Programmers: 1, 2, AUTOMATION TIME!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Think Python

Think Python

Allen B. Downey / O'Reilly Media / 2012-8-23 / GBP 29.99

Think Python is an introduction to Python programming for students with no programming experience. It starts with the most basic concepts of programming, and is carefully designed to define all terms ......一起来看看 《Think Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换