Linux Bash vs. Windows PowerShell

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

内容简介: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!!

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

查看所有标签

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

激荡十年,水大鱼大

激荡十年,水大鱼大

吴晓波 / 中信出版社 / 2017-11-1 / CNY 58.00

【编辑推荐】 知名财经作者吴晓波新作,畅销十年、销量超过两百万册的《激荡三十年》续篇,至此完成改革开放四十年企业史完整记录。 作为时代记录者,吴晓波有意识地从1978年中国改革开放伊始,记录中国翻天覆地的变化和对我们影响至深的人物与事件,串成一部我们每个人的时代激荡史。而最新的这十年,无疑更壮观,也更扑朔迷离。 很多事情,在当时并未有很深很透的感受,回过头来再看,可能命运的轨迹就......一起来看看 《激荡十年,水大鱼大》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

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

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具