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!!

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

查看所有标签

猜你喜欢:

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

产品经理修炼之道

产品经理修炼之道

费杰 / 机械工业出版社华章公司 / 2012-7-30 / 59.00元

本书由资深产品经理、中国最大的产品经理沙龙Pmcaff创始人费杰亲自执笔,微软、腾讯、百度、新浪、搜狐、奇虎、阿里云、Evernote等国内外20余家大型互联网企业资深产品经理和技术专家联袂推荐。用系统化的方法论和丰富的实战案例解读了优秀产品经理所必须修炼的产品规划能力、产品设计能力、产品执行能力,以及思考、分析和解决问题的能力和方法,旨在为互联网产品经理打造核心竞争力提供实践指导。 全书一......一起来看看 《产品经理修炼之道》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具