注:本文为 “Bash vs PowerShell” 相关合辑。
英文引文,机翻未校。
未整理去重。
如有内容异常,请看原文。
Difference Between CMD vs PowerShell vs Bash
CMD、PowerShell 与 Bash 之间的区别
Written by Uzair Ghalib on the 13th Dec 2024
If you are a tech guy or have any kind of relation with IT, you must have come across the comparisons between CMD, PowerShell, Bash, and Windows vs Linux/Unix.
如果你是一个技术人员,或者与 IT 有任何关联,你一定遇到过关于 CMD、PowerShell、Bash 以及 Windows 与 Linux/Unix 的比较。
There is usually a debate going on about which tool is better for performing different administration and server automation tasks. Moreover, many people still couldn’t get over the difference between PowerShell, CMD, and Bash.
关于哪种工具更适合执行不同的管理和服务器自动化任务,通常存在争议。此外,许多人仍然无法搞清楚 PowerShell、CMD 和 Bash 之间的区别。
A lot of comparisons are carried out each day about their functionality and features, but still, it’s a hot topic.
每天都有很多关于它们功能和特性的比较,但这仍然是一个热门话题。
This article will introduce all three tools and draw a clear line about how they are different from each other.
本文将介绍这三种工具,并明确它们之间的区别。
Comparison Table of CMD vs PowerShell vs Bash
CMD、PowerShell 与 Bash 对比表
Feature | CMD | PowerShell | Bash |
---|---|---|---|
Description 描述 | CMD is the command line for Microsoft Windows operating system, with command-based features. CMD 是 Microsoft Windows 操作系统的命令行,具有基于命令的功能。 | PowerShell is a task-based command-line interface, specifically designed for system admins and is based on the .Net Framework. PowerShell 是一个基于任务的命令行界面,专为系统管理员设计,基于 .Net 框架。 | Bash is a command-line and scripting language for most Unix/Linux-based operating systems. Bash 是大多数 Unix/Linux 操作系统的命令行和脚本语言。 |
Output Type 输出类型 | The generated output is in the form of characters. 生成的输出为字符形式。 | PowerShell treats input and output as objects. PowerShell 将输入和输出视为对象。 | Like CMD, Bash also treats input and output as text structures. 和 CMD 一样,Bash 也将输入和输出视为文本结构。 |
Interface 界面 | CMD uses a text-based command-line interface. CMD 使用基于文本的命令行界面。 | PowerShell has a more interactive graphical command-line interface CLI. PowerShell 具有更具交互性的图形化命令行界面(CLI)。 | Bash has a simple user interface that is also text-based like CMD. Bash 具有简单的用户界面,也像 CMD 一样是基于文本的。 |
Availability 可用性 | It comes as a default program in all the windows operating systems. 它是所有 Windows 操作系统中的默认程序。 | In windows 2007 and above, PowerShell is a built-in program. Before that, it has to be installed as a separate program. 在 Windows 2007 及以上版本中,PowerShell 是内置程序。在此之前,它必须作为单独的程序安装。 | Linux and Unix systems mostly use Bash, and it is used from the very first day in Linux and Unix operating systems. Linux 和 Unix 系统大多使用 Bash,从 Linux 和 Unix 操作系统诞生之初就开始使用了。 |
Platform 平台 | Windows only. 仅 Windows。 | Cross-platform (Windows, Linux, macOS). 跨平台(Windows、Linux、macOS)。 | Unix/Linux, macOS, and Windows via WSL. Unix/Linux、macOS 以及通过 WSL 在 Windows 上使用。 |
Scripting Support 脚本支持 | Basic batch scripting capabilities. 基本的批处理脚本功能。 | Advanced scripting, including error handling and object manipulation. 高级脚本编写,包括错误处理和对象操作。 | Robust shell scripting with text processing utilities like grep , awk , and sed .强大的 shell 脚本,带有 grep 、awk 和 sed 等文本处理工具。 |
Integration 集成 | Limited to basic Windows tasks. 仅限于基本的 Windows 任务。 | Deep integration with Windows APIs, registry, and services. 与 Windows API、注册表和服务深度集成。 | Deep integration with Unix/Linux systems. 与 Unix/Linux 系统深度集成。 |
Ease of Use 易用性 | Simple and lightweight. 简单且轻量。 | Moderate complexity but powerful. 中等复杂度但功能强大。 | Moderate complexity, especially for advanced scripting. 中等复杂度,特别是对于高级脚本编写而言。 |
Use Case 使用场景 | Legacy support and basic Windows tasks. 遗留支持和基本的 Windows 任务。 | Advanced automation, Windows management, and system administration. 高级自动化、Windows 管理和系统管理。 | Unix/Linux system-level tasks, cross-platform scripting, and file manipulation. Unix/Linux 系统级任务、跨平台脚本编写和文件操作。 |
What is CMD?
什么是 CMD?
CMD stands for Windows Command Prompt. When Microsoft DOS operating system was launched initially, CMD was used as a shell in it and the first version of CMD was released in 1987, 34 years from today. The command prompt is the default command-line interpreter for Microsoft Windows and few other operating systems.
CMD 代表 Windows 命令提示符。当 Microsoft DOS 操作系统最初推出时,CMD 被用作其中的 shell,CMD 的第一个版本于 1987 年发布,距今已有 34 年。命令提示符是 Microsoft Windows 和少数其他操作系统的默认命令行解释器。
CMD is usually used for carrying out different routine administration tasks and is also used to perform automation. All such tasks are performed using scripts and batch files in the command prompt. Command prompt has pre-built commands that are usually used to perform different operations such as CD, copy, paste, ping, etc.
CMD 通常用于执行各种日常管理任务,也用于实现自动化。所有这些任务都是在命令提示符中通过脚本和批处理文件来执行的。命令提示符有一些预构建的命令,通常用于执行不同的操作,如 CD、复制、粘贴、ping 等。
Example Commands:
示例命令:
-
dir
(list directory contents)dir
(列出目录内容) -
copy
(copy files)copy
(复制文件) -
del
(delete files)del
(删除文件)
What is PowerShell?
什么是 PowerShell?
Microsoft Windows was never considered to be very good at task automation compared to Linux. It was because of the limited functionality of the command prompt. In 2002 Microsoft started working on a new command-line tool and it was called Monad. In 2006, the first version was released with the name PowerShell.
与 Linux 相比,Microsoft Windows 在任务自动化方面向来不被认为很出色。这是因为命令提示符的功能有限。2002 年,微软开始研发一种新的命令行工具,名为 Monad。2006 年,第一个版本以 PowerShell 的名称发布。
PowerShell is an automated command-line shell and also has all the features & functionalities of CMD. The major update or difference in PowerShell is that it is object-oriented. Its functionality is based on objects, very similar to object-oriented programming based on objects, functions, and methods. One great addition to the list of features in PowerShell is command lets, usually called Cmdlets.
PowerShell 是一个自动化的命令行 shell,还具备 CMD 的所有特性和功能。PowerShell 的主要更新或不同之处在于它是面向对象的。其功能基于对象,与基于对象、函数和方法的面向对象编程非常相似。PowerShell 功能列表中的一个重要补充是命令小程序,通常称为 Cmdlets。
Example Commands:
示例命令:
-
Get-Process
(list running processes)Get-Process
(列出正在运行的进程) -
Get-Content file.txt | ForEach-Object { $_.ToUpper() }
(process file contents)Get-Content file.txt | ForEach-Object { $_.ToUpper() }
(处理文件内容) -
Set-Service -Name "spooler" -StartupType Automatic
(manage services)Set-Service -Name "spooler" -StartupType Automatic
(管理服务)
What is Bash?
什么是 Bash?
As PowerShell is popular in windows operating systems, Bash is one of the most popular command-line interfaces in UNIX. Moreover, Bash is the default Shell these days in most Unix/Linux-based systems. In 1977 Stephen Bourne released his shell, at that time no shell was freely available.
正如 PowerShell 在 Windows 操作系统中很受欢迎一样,Bash 是 UNIX 中最受欢迎的命令行界面之一。此外,如今在大多数基于 Unix/Linux 的系统中,Bash 是默认的 Shell。1977 年,Stephen Bourne 发布了他的 shell,当时没有免费可用的 shell。
Stephen Bourne released another open-source shell named (Bash) Bourne Again Shell in 1988 for the GNU project to get over this limitation. Since then, Bash is extremely popular among developers specially for deploying their code base or applications. Some of the key features of Bash are system stability, command-line editing, key-binding, command history, open-source, etc. All these features make it one of the most used scripting environments among IT professionals.
为了克服这一限制,Stephen Bourne 于 1988 年为 GNU 项目发布了另一个开源 shell,名为(Bash)Bourne Again Shell。从那以后,Bash 在开发人员中非常受欢迎,特别是在部署他们的代码库或应用程序方面。Bash 的一些关键特性包括系统稳定性、命令行编辑、键绑定、命令历史、开源等。所有这些特性使其成为 IT 专业人员中使用最广泛的脚本环境之一。
Example Commands:
示例命令:
-
ls -l
(list directory contents)ls -l
(列出目录内容) -
cat file.txt | grep "error"
(filter lines containing “error”)cat file.txt | grep "error"
(过滤包含“error”的行) -
for file in *.txt; do echo $file; done
(iterate over files)for file in *.txt; do echo $file; done
(遍历文件)
Final Words
结语
To put it in a nutshell, each of the tools has its own benefits and usage according to the specific circumstances. PowerShell is the advanced version of CMD with a lot of added functionalities and features. Moreover, it has a better graphical interface and better support for server automation tasks. Comparing PowerShell and Bash is like comparing apples and oranges, they are used for different operating systems with different functionalities all over.
简而言之,每种工具根据具体情况都有其自身的优势和用途。PowerShell 是 CMD 的高级版本,具有许多附加的功能和特性。此外,它具有更好的图形界面,对服务器自动化任务的支持也更好。比较 PowerShell 和 Bash 就像比较苹果和橙子,它们用于不同的操作系统,功能各异。
Post Written by Uzair Ghalib
Bash vs PowerShell: Choosing the Right Scripting Shell
Bash 与 PowerShell:选择合适的脚本 Shell
Have you ever found yourself automating a task on your computer, or perhaps trying to manage multiple servers at once? If so, you’ve likely dabbled in the world of scripting shells. Think of these as powerful toolkits, allowing you to converse with your computer in a more advanced dialect than the usual point-and-click.
你是否曾经需要在电脑上自动执行某项任务,或者尝试同时管理多台服务器?如果是这样,你可能已经涉足了脚本 Shell 的世界。可以把它们看作强大的工具包,让你能够用一种比常见的点击操作更高级的“语言”与电脑交流。
In today’s digital age, the ability to script and automate tasks isn’t just a fancy skill; it’s often a necessity. As our tasks become more complex and our environments more diverse, the tools we choose to interact with these systems become even more crucial. Two of the giants in this domain are Bash and PowerShell. But how do they stack up against each other? Which one should you invest your time in? Let’s dive into their histories to get a clearer picture.
在当今的数字时代,编写脚本和自动化任务的能力不仅仅是一项酷炫的技能,往往更是一种必要能力。随着我们的任务变得越来越复杂,使用的环境也越来越多样化,我们选择用来与这些系统交互的工具就变得愈发关键。在这个领域,有两个巨头——Bash 和 PowerShell。但它们各自的优势是什么?你应该把时间投入到哪一个上呢?让我们从它们的历史说起,以便更清晰地了解。
History and Background
历史与背景
Bash (Bourne Again Shell)
In the late 1980s, a new shell walked into the UNIX scene, christened as the Bourne Again Shell, or Bash for short. It was conceived as a free replacement for the Bourne shell (sh) found in UNIX systems. Crafted by Brian Fox for the GNU Project, Bash quickly found its home in most Linux distributions. Over the decades, it’s become the default command-line interface for countless users and has grown synonymous with the Linux experience itself.
20 世纪 80 年代末,一种新的 Shell 进入了 UNIX 领域,被命名为 Bourne Again Shell,简称 Bash。它最初是作为 UNIX 系统中 Bourne shell(sh)的免费替代品而设计的。由 Brian Fox 为 GNU 项目开发,Bash 很快在大多数 Linux 发行版中占据了一席之地。几十年来,它已经成为无数用户的默认命令行界面,并且几乎成了 Linux 使用体验的代名词。
PowerShell
Shift your gaze from the open prairies of Linux to the skyscrapers of Windows, and you’ll find PowerShell reigning supreme. Birthed by Microsoft in the mid-2000s, PowerShell was designed to be more than just a shell; it aimed to be a complete automation framework. Melding the command-line functionalities of traditional shells with the object-oriented capabilities of .NET, PowerShell presented Windows admins and developers with a powerful tool. Originally tied closely to Windows, it’s now grown wings and gone cross-platform, thanks to its newer iteration, PowerShell Core.
将目光从 Linux 的广阔天地转向 Windows 的“高楼大厦”,你会发现 PowerShell 占据着主导地位。PowerShell 由微软在 21 世纪中期推出,它的设计初衷不仅仅是一个 Shell,更是一个完整的自动化框架。它融合了传统 Shell 的命令行功能和 .NET 的面向对象能力,为 Windows 管理员和开发人员提供了一个强大的工具。最初它与 Windows 紧密绑定,但凭借其更新的版本 PowerShell Core,如今已经实现了跨平台。
Key Features
主要特性
Bash
Command-line utilities and tools
命令行实用程序和工具
If you’ve ever typed commands like grep
, awk
, or sed
, you’re harnessing the power of Bash’s vast toolbox. These utilities are the bread and butter for text processing and data manipulation in UNIX-like systems.
如果你曾经输入过 grep
、awk
或 sed
这样的命令,那么你正在利用 Bash 庞大工具库的力量。这些实用程序是类 UNIX 系统中文本处理和数据操作的核心工具。
Scripting syntax and capabilities
脚本语法和功能
Bash scripting allows for variables, loops, and conditional executions, making it a potent tool for automation. If you can think of a task, you can likely script it in Bash.
Bash 脚本支持变量、循环和条件执行,使其成为自动化的有力工具。只要你能想到的任务,几乎都可以用 Bash 脚本来实现。
Piping and redirection
管道和重定向
Bash’s strength lies in its simplicity. You can take the output of one command and use it as the input for another, seamlessly linking tasks together. This is achieved through pipes (|
) and redirection operators (>
, >>
, etc.).
Bash 的优势在于其简洁性。你可以将一个命令的输出作为另一个命令的输入,无缝地将任务链接起来。这通过管道(|
)和重定向运算符(>
, >>
等)来实现。
PowerShell
Object-oriented nature and .NET integration
面向对象特性和 .NET 集成
Unlike traditional shells that work primarily with text, PowerShell works with objects. This means you’re not just seeing the text output of a command; you’re seeing structured data that can be manipulated in many powerful ways.
与主要处理文本的传统 Shell 不同,PowerShell 处理的是对象。这意味着你看到的不仅仅是命令的文本输出,更是可以通过多种强大方式进行操作的结构化数据。
Cmdlets and advanced functions
Cmdlet 和高级函数
In PowerShell, commands are often referred to as ‘cmdlets’ (pronounced “command-lets”). They’re designed to be used in the pipeline, much like Bash, but operate on .NET objects. This provides a blend of simple and advanced functionalities.
在 PowerShell 中,命令通常被称为“cmdlet”(发音为“command-lets”)。它们像 Bash 中的命令一样可以在管道中使用,但操作的是 .NET 对象。这使得它兼具简单和高级功能。
Remote management capabilities
远程管理能力
With PowerShell, managing a machine from afar is as easy as doing it locally. Its built-in cmdlets allow you to run commands on remote computers with minimal fuss.
使用 PowerShell,远程管理一台机器就像在本地管理一样简单。其内置的 cmdlet 让你可以轻松地在远程计算机上运行命令。
Usability and Learning Curve
易用性和学习曲线
Bash
Pros
优点
With its long history and widespread adoption, Bash has an ocean of tutorials, forums, and communities. If you’re diving into the Linux world, you’ll find Bash as a ubiquitous companion.
由于其悠久的历史和广泛的应用,Bash 拥有海量的教程、论坛和社区。如果你进入 Linux 领域,会发现 Bash 无处不在。
Cons
缺点
For those new to scripting or coming from other platforms, Bash’s syntax can sometimes seem a tad cryptic. It might take a moment to get accustomed to its unique nuances.
对于脚本新手或来自其他平台的人来说,Bash 的语法有时会显得有些晦涩难懂。可能需要一些时间才能适应其独特的细微差别。
PowerShell
Pros
优点
If you’re already familiar with the Microsoft ecosystem or come from a .NET background, PowerShell will feel like home. Its syntax is designed to be intuitive, especially for those who’ve dabbled in C# or other .NET languages.
如果你已经熟悉微软生态系统或有 .NET 背景,PowerShell 会让你倍感亲切。其语法设计直观,尤其对于那些接触过 C# 或其他 .NET 语言的人来说更是如此。
Cons
缺点
For die-hard UNIX enthusiasts or those deeply rooted in other scripting languages, the initial plunge into PowerShell can feel a bit jarring. It’s a departure from the traditional text-based approach, and its verbosity can sometimes be a double-edged sword.
对于忠实的 UNIX 爱好者或深耕其他脚本语言的人来说,刚开始接触 PowerShell 可能会有些不适应。它偏离了传统的基于文本的方式,而且其冗长性有时也是一把双刃剑。
Extensibility and Community Support
可扩展性和社区支持
Bash
Availability of tools and plugins
工具和插件的可用性
Bash’s legacy in the Linux and UNIX environment means there’s a plethora of tools, plugins, and extensions available. Whether it’s package management tools like apt
or yum
, or plugins to enhance your terminal experience, Bash has you covered.
Bash 在 Linux 和 UNIX 环境中的悠久历史意味着有大量的工具、插件和扩展可用。无论是 apt
或 yum
这样的包管理工具,还是增强终端体验的插件,Bash 都能满足你的需求。
Open-source community contributions
开源社区贡献
The open-source nature of many UNIX-like systems has fostered a thriving community around Bash. This means continuous improvements, a vast array of user-contributed scripts, and a helping hand always available in online forums and communities.
许多类 UNIX 系统的开源性质培育了一个围绕 Bash 的活跃社区。这意味着持续的改进、大量用户贡献的脚本,以及在在线论坛和社区中随时可得的帮助。
PowerShell
Modules and the PowerShell Gallery
模块和 PowerShell 库
PowerShell is not just a shell; it’s an ecosystem. Modules are packages of cmdlets, and the PowerShell Gallery is a repository of these modules. Whether you need tools for Azure, AWS, or SQL Server, there’s probably a module for it.
PowerShell 不仅仅是一个 Shell,更是一个生态系统。模块是 cmdlet 的集合,而 PowerShell 库 是这些模块的仓库。无论你需要用于 Azure、AWS 还是 SQL Server 的工具,很可能都有相应的模块。
Microsoft’s active development and community engagement
微软的积极开发和社区参与
With Microsoft at the helm, PowerShell benefits from regular updates, extensive documentation, and direct engagement with its user base. Microsoft’s push towards open source with PowerShell Core has further amplified community contributions and cross-platform capabilities.
在微软的主导下,PowerShell 拥有定期更新、详尽的文档以及与用户群体的直接互动。微软通过 PowerShell Core 向开源的推进,进一步促进了社区贡献和跨平台能力的提升。
Cross-Platform Capabilities
跨平台能力
Bash
Native to UNIX-based systems
基于 UNIX 系统的原生工具
Originally developed for UNIX, Bash naturally feels at home on UNIX-based systems like Linux. Over time, it’s also made its way into macOS, which is UNIX-certified.
Bash 最初为 UNIX 开发,因此在 Linux 等基于 UNIX 的系统上自然如鱼得水。随着时间的推移,它也进入了经过 UNIX 认证的 macOS 系统。
Available on Windows
在 Windows 上可用
With tools like the Windows Subsystem for Linux (WSL), Bash has carved out a space for itself in the Windows environment. While it’s not native, tools like WSL and Git Bash make it accessible for those who need it.
借助 Windows Subsystem for Linux(WSL)等工具,Bash 在 Windows 环境中也占据了一席之地。虽然它不是原生的,但 WSL 和 Git Bash 等工具让有需要的人能够使用它。
PowerShell
From Windows to the World
从 Windows 走向世界
PowerShell was born and raised in the Windows environment. But with PowerShell Core, its latest iteration, it’s shed its Windows-only skin. It’s now cross-platform and can be run on Linux and macOS, making it a versatile tool no matter your OS of choice.
PowerShell 诞生并成长于 Windows 环境。但凭借其最新版本 PowerShell Core,它摆脱了仅支持 Windows 的限制。现在它是跨平台的,可以在 Linux 和 macOS 上运行,无论你选择哪种操作系统,它都是一个多功能的工具。
Performance and Efficiency
性能和效率
Bash
Lightweight and Fast
轻量且快速
Bash is designed for performance. Its lightweight nature ensures quick startup times and brisk execution. When combined with native UNIX tools like grep
, awk
, and sed
, text processing and data manipulation tasks are notably efficient.
Bash 专为性能而设计。其轻量级特性确保了快速的启动时间和执行速度。当与 grep
、awk
和 sed
等原生 UNIX 工具结合使用时,文本处理和数据操作任务效率极高。
Resource Consumption
资源消耗
Given its minimalist design, Bash tends to be light on system resources. It’s designed to run on systems with minimal hardware, making it ideal for older machines or lightweight Linux distributions.
由于其极简的设计,Bash 对系统资源的消耗较少。它旨在在硬件配置较低的系统上运行,非常适合旧机器或轻量级 Linux 发行版。
PowerShell
Powerful but Resource-Intensive
功能强大但资源消耗较大
PowerShell, with its .NET foundation, brings a powerful toolset to the table. This strength, however, comes with increased resource consumption, especially when handling large sets of objects or complex tasks.
基于 .NET 框架的 PowerShell 带来了强大的工具集。然而,这种优势也伴随着资源消耗的增加,特别是在处理大量对象或复杂任务时。
Optimized for Advanced Tasks
针对高级任务进行了优化
Where Bash shines in quick, text-based operations, PowerShell excels in intricate, object-oriented tasks, especially those tied to the Windows ecosystem like Active Directory manipulations or advanced system diagnostics.
Bash 在快速的基于文本的操作中表现出色,而 PowerShell 则在复杂的面向对象任务中表现卓越,特别是与 Windows 生态系统相关的任务,如 Active Directory 操作或高级系统诊断。
Security
安全性
Bash
Elevation with ‘sudo’
通过 ‘sudo’ 提升权限
The sudo
command in Bash allows users to run specific tasks with elevated privileges, providing a mechanism for controlled access to critical system functions.
Bash 中的 sudo
命令允许用户以提升的权限运行特定任务,为关键系统功能的受控访问提供了一种机制。
Common Security Concerns
常见的安全问题
Like all tools, Bash isn’t immune to vulnerabilities. Common concerns include script injection, improper permissions, and unprotected environment variables. However, the active open-source community ensures patches and updates are regularly rolled out.
与所有工具一样,Bash 也并非没有漏洞。常见的问题包括脚本注入、权限设置不当以及未受保护的环境变量。不过,活跃的开源社区确保了补丁和更新的定期推出。
PowerShell
Execution Policies
执行策略
PowerShell offers a layered approach to script execution. By setting execution policies, administrators can control the conditions under which PowerShell scripts run, offering a safeguard against unauthorized or malicious scripts.
PowerShell 对脚本执行采用分层控制方法。通过设置执行策略,管理员可以控制 PowerShell 脚本 的运行条件,为防范未授权或恶意脚本提供保障。
Secure Remote Management
安全的远程管理
With features like Just Enough Administration (JEA) and PowerShell Remoting over SSH, PowerShell offers robust tools for secure remote task execution and system management.
凭借 Just Enough Administration(JEA)和基于 SSH 的 PowerShell 远程管理等功能,PowerShell 为安全的远程任务执行和系统管理提供了强大的工具。
Auditing and Logging
审计和日志记录
One of PowerShell’s strengths in enterprise environments is its extensive logging capabilities. Administrators can audit and monitor executed commands, providing a clear trace of activities and potential breaches.
在企业环境中,PowerShell 的优势之一是其强大的日志记录功能。管理员可以审计和监控已执行的命令,清晰追踪活动和潜在的安全漏洞。
Real-World Applications and Use Cases
实际应用场景
Bash
Server Management and Automation
服务器管理和自动化
From setting up cron jobs to orchestrating server tasks, Bash scripts play a pivotal role in Linux server management. System administrators often use Bash scripts to automate backups, monitor system health, and manage user accounts.
从设置 cron 作业到协调服务器任务,Bash 脚本在 Linux 服务器管理中发挥着关键作用。系统管理员经常使用 Bash 脚本来自动执行备份、监控系统健康状况和管理用户账户。
Pipeline Data Processing
管道数据处理
Need to filter logs, process text files, or transform data formats? Bash, combined with tools like awk
, sed
, and grep
, offers a streamlined way to handle data on the fly.
需要过滤日志、处理文本文件或转换数据格式吗?Bash 与 awk
、sed
和 grep
等工具结合,提供了一种高效的实时数据处理方式。
Embedded Systems
嵌入式系统
由于其轻量级特性,Bash 广泛应用于嵌入式系统和物联网设备中,在资源有限的环境中不可或缺。
PowerShell
Windows System Administration
Windows 系统管理
无论是在 Active Directory 中管理用户角色、自动执行 Windows 更新,还是处理 IIS(Internet Information Services)任务,PowerShell 都是 Windows 管理员的首选工具。
Cloud Management
云管理
借助为 Azure、AWS 和其他云平台量身定制的模块,PowerShell 为云编排和自动化提供了全面的工具集。
Cross-platform Scripting
跨平台脚本编写
PowerShell Core 的跨平台特性使开发人员和系统管理员能够编写在 Windows、Linux 和 macOS 上无缝运行的脚本,确保功能一致性并减少平台特定的开销。
Conclusion
结论
In the vibrant landscape of scripting shells, both Bash and PowerShell emerge as powerful contenders, each with its own set of strengths and nuances. Bash, with its roots deep in the UNIX world, offers a lightweight and versatile toolset, making it a favorite among Linux enthusiasts and sysadmins. On the other hand, PowerShell, backed by Microsoft’s vast ecosystem, excels in object-oriented tasks and offers deep integration with Windows and cloud platforms.
在充满活力的脚本 Shell 领域,Bash 和 PowerShell 都是强大的竞争者,各自拥有一系列优势和特点。Bash 深深植根于 UNIX 世界,提供轻量级且多功能的工具集,深受 Linux 爱好者和系统管理员的喜爱。另一方面,在微软庞大生态系统支持下的 PowerShell,在面向对象任务中表现出色,并与 Windows 和云平台深度集成。
Choosing between Bash and PowerShell isn’t a matter of which is ‘better,’ but rather which aligns more with your specific needs, environment, and comfort zone. If you’re diving deep into the Linux realm, Bash is an undeniable ally. But if you’re navigating the expansive corridors of the Windows world or managing hybrid cloud environments, PowerShell might be your weapon of choice.
选择 Bash 还是 PowerShell,并非取决于哪个“更好”,而是哪个更符合你的特定需求、使用环境和舒适区。如果你深入 Linux 领域,Bash 无疑是一个好帮手。但如果你在 Windows 世界的广阔领域中探索,或者管理混合云环境,PowerShell 可能是你的首选工具。
Remember, in the world of technology, it’s often not about picking sides but about having the right tool for the job. And sometimes, that means knowing a bit of both!
记住,在技术世界中,往往不在于选边站,而在于拥有适合特定工作的工具。有时,这意味着你需要对两者都有所了解!
Shell vs BASH vs PowerShell vs CMD
Shell 与 BASH 与 PowerShell 与 CMD
A. Yigit Ogun
Today I was talking with one of my friend about a shell script. I noticed that she doesn’t comprehensively understand what are the difference in between Shell and PowerShell .Besides she even thought that Shell and Bash are the same thing. Her name is confidential, lol. So, I decided to publish an article about this topic.
今天,我和我的一个朋友谈论了一个 shell 脚本。我注意到她并不全面了解 Shell 和 PowerShell 之间的区别。此外,她甚至认为 Shell 和 Bash 是一回事。她的名字是保密的,哈哈。所以,我决定发表一篇关于这个主题的文章。
A shell is a program that accepts input from a keyboard and hands it off to the OS. As you type commands, the shell interprets them such that the OS can understand them. A shell is software that allows users of an operating system to access the kernel’s services through a user interface. A shell can be invoked by the shell command in the command-line interface (CLI) on Unix- or Linux-based OS, letting people manage operations via specific commands, text, or script. Programming languages have their shells, which provide them independence from the operating system and allow cross-platform interoperability. Let’s dive in!
shell 是一个程序,它接受来自键盘的输入并将其传递给操作系统。键入命令时,shell 会解释这些命令,以便操作系统可以理解它们。shell 是允许操作系统用户通过用户界面访问内核服务的软件。在基于 Unix 或 Linux 的操作系统上,可以通过命令行界面(CLI)中的 shell 命令调用 shell,让人们通过特定命令、文本或脚本管理操作。编程语言有自己的 shell,这使它们独立于操作系统并允许跨平台互操作性。让我们开始吧!
What is the Shell(sh)(Bourne Shell)?
什么是 Shell(sh)(Bourne Shell)?
The Unix operating system has a command-line interface known as a Shell also known as Bourne Shell. It collects data from you and runs programs depending on that data. The output of a program is displayed once it has completed its execution. Shell is a command-line interface that allows us to run commands, programs, and shell scripts. Shells come in a variety of flavours, much as operating systems come in a variety of flavours.
Unix 操作系统有一个称为 Shell 的命令行界面,也称为 Bourne Shell。它从你那里收集数据并根据这些数据运行程序。程序完成执行后,将显示程序的输出。Shell 是一个命令行界面,允许我们运行命令、程序和 shell 脚本。Shell 有多种风格,就像操作系统有多种风格一样。
Bourne shell interaction on Version 7 Unix
Each Shell has its own set of commands and functionalities that are well-known. A shell script is just a set of commands that are listed in the sequence in which they should be executed. Comments, preceded by the # symbol, describe the steps of a decent shell script.
每个 Shell 都有自己的一组众所周知的命令和功能。shell 脚本只是一组命令,这些命令按应执行的顺序列出。注释(前面有 # 符号)描述了一个不错的 shell 脚本的步骤。
The shell issues the prompt, $, sometimes known as the command prompt. You can type a command while the prompt is shown. After you press Enter, Shell reads your input. It looks at the first word of your input to decide which command you wish to run. A word is a continuous string of characters. Words are separated by spaces and tabs. Shell scripts must include certain mandatory structures that instruct the shell environment what to do and when. Most scripts, however, are more complicated than the one above.
shell 发出提示符 $,有时称为命令提示符。你可以在显示提示符时键入命令。按 Enter 键后,Shell 会读取你的输入。它会查看你输入的第一个单词,以决定要运行哪个命令。单词是连续的字符串。单词之间用空格和制表符分隔。Shell 脚本必须包含某些强制性结构,这些结构指示 shell 环境执行哪些操作以及何时执行。但是,大多数脚本都比上面的脚本更复杂。
After all, the Shell is a full-fledged programming language with variables, control structures, and so on. A script is still just a series of commands executed in order, no matter how complex it becomes.
毕竟,Shell 是一种成熟的编程语言,具有变量、控制结构等。脚本仍然只是按顺序执行的一系列命令,无论它变得多么复杂。
What is Bash(UNIX Shell)?
什么是 Bash(UNIX Shell)?
BASH stands for Bourne Again Shell, a clever term that pays homage to Bourne Shell. Bash is a shell program developed by Brian Fox as an update to the Bourne Shell application ‘sh.’ It’s a GNU project that’s open source. It was first introduced in 1989 as one of the most popular GNU/Linux shell distributions. It is more functional than Bourne Shell in terms of programming and interactive use.
BASH 代表 Bourne Again Shell,这是一个向 Bourne Shell 致敬的巧妙术语。Bash 是由 Brian Fox 开发的一个 shell 程序,作为 Bourne Shell 应用程序“sh”的更新。这是一个开源的 GNU 项目。它于 1989 年首次推出,作为最流行的 GNU/Linux shell 发行版之一。在编程和交互式使用方面,它比 Bourne Shell 功能更强大。
It offers features such as command-line editing, key bindings, and endless command history. Bash is a command-line interpreter that runs in a text window and allows the user to interpret commands to do various tasks. A Shell Script is a sequence of these commands contained within a single file. A Shell Script can be read and executed using Bash.
它提供命令行编辑、键绑定和无尽的命令历史记录等功能。Bash 是一个命令行解释器,它在文本窗口中运行,允许用户解释命令以执行各种任务。Shell 脚本是包含在单个文件中的这些命令的序列。可以使用 Bash 读取和执行 Shell 脚本。
Brian Fox — Author of GNU Bash
Bash is compatible with sh since it is based on the original UNIX Bourne Shell. It has the best and most useful features of the Korn and C shells, such as directory manipulation, job control, aliases, and so on. Key bindings in Bash allow you to create customized editing key sequences. Bash has one-dimensional arrays that make it simple to access and handle data lists.
Bash 与 sh 兼容,因为它基于原始的 UNIX Bourne Shell。它具有 Korn 和 C shell 的最佳和最有用的功能,例如目录操作、作业控制、别名等。Bash 中的键绑定允许你创建自定义的编辑键序列。Bash 具有一维数组,使访问和处理数据列表变得简单。
The history of recently-visited directories within a list is specified by Directory Stack in Bash. For example, the pushd built-in is used to add a directory to the stack, the popd built-in is used to remove a directory from the stack, and the dirs built-in is used to display the directory stack’s content. For most Linux distributions and Apple’s macOS, Bash is the default login shell. It’s also available for Windows 10 with a Solaris 11 version and default user shell.
列表中最近访问的目录的历史记录由 Bash 中的 Directory Stack 指定。例如,pushd 内置用于向堆栈中添加目录,popd 内置用于从堆栈中删除目录,dirs 内置用于显示目录堆栈的内容。对于大多数 Linux 发行版和 Apple 的 macOS,Bash 是默认的登录 shell。它还适用于具有 Solaris 11 版本和默认用户 shell 的 Windows 10。
Screenshot of a Bash session
Bash 会话的屏幕截图
Bash Features
Bash 功能
Bash provides a lot of flexibility and syntax that looks a lot like those of modern programming languages. Some of the notable features introduced in Bash are:
Bash 提供了许多灵活性和语法,看起来与现代编程语言非常相似。Bash 中引入的一些值得注意的功能包括:
- Command-line completion can be used to quickly complete a command using the key
命令行补全可用于使用 键快速补全命令 - Command history through which we can quickly search for commands previously executed by using the arrow key or
命令历史记录,通过它我们可以使用 箭头键或 快速搜索以前执行的命令 - Arithmetic evaluation without having to use external programs
无需使用外部程序即可进行算术计算 - Associative arrays, which enables us to create an array with string indices
关联数组,它使我们能够创建一个带有字符串索引的数组 - Keyboard shortcuts for command-line editing
用于命令行编辑的键盘快捷键 - Customizability enables us to modify the default presentation offered by Bash
可定制性使我们能够修改 Bash 提供的默认显示方式
What is CMD(Command Prompt)?
什么是 CMD(命令提示符)?
CMD stands for Windows Command Prompt. When Microsoft DOS operating system was launched initially, CMD was used as a shell in it and the first version of CMD was released in 1987. The command prompt is the default command-line interpreter for Microsoft Windows and few other operating systems. CMD is usually used for carrying out different routine administration tasks and is also used to perform automation. All such tasks are performed using scripts and batch files in the command prompt. Command prompt has pre-built commands that are usually used to perform different operations such as CD, copy, paste, ping, etc.
CMD 代表 Windows 命令提示符。当 Microsoft DOS 操作系统最初推出时,CMD 被用作其中的 shell,CMD 的第一个版本于 1987 年发布。命令提示符是 Microsoft Windows 和少数其他操作系统的默认命令行解释器。CMD 通常用于执行不同的日常管理任务,也用于执行自动化。所有此类任务都是在命令提示符中使用脚本和批处理文件执行的。命令提示符具有预构建的命令,通常用于执行不同的操作,例如 CD、复制、粘贴、ping 等。
What is PowerShell?
什么是 PowerShell?
Welcome to the Microsoft side of the world. Microsoft Windows was never considered to be very good at task automation compared to Linux. It was because of the limited functionality of the command prompt. In 2002 Microsoft started working on a new command-line tool and it was called Monad. In 2006, the first version was released with the name Powershell. Powershell is an automated command-line shell and also has all the features & functionalities of CMD. The major update or difference in Powershell is that it is object-oriented. Its functionality is based on objects, very similar to object-oriented programming based on objects, functions, and methods. One great addition to the list of features in Powershell is command lets, usually called Cmdlets.
与 Linux 相比,Microsoft Windows 从来都不被认为非常擅长任务自动化。这是因为命令提示符的功能有限。2002 年,Microsoft 开始开发一种新的命令行工具,它被称为 Monad。2006 年,第一个版本以 Powershell 的名称发布。Powershell 是一个自动化的命令行 shell,也具有 CMD 的所有特性和功能。Powershell 的主要更新或差异在于它是面向对象的。它的功能基于对象,与基于对象、函数和方法的面向对象编程非常相似。Powershell 功能列表的一个很好的补充是命令小程序,通常称为 Cmdlets。
SUMMARY
总结
To put it in a nutshell, each of the tools has its own benefits and usage according to the specific circumstances. Powershell is the advanced version of CMD with a lot of added functionalities and features. Moreover, it has a better graphical interface and better support for server automation tasks. And Bash is same for Shell. Comparing Powershell and Bash is like comparing apples and oranges, they are used for different operating systems with different functionalities all over. Thank you for reading and I hope it helps somebody out there.
简而言之,每种工具根据具体情况都有自己的好处和用途。Powershell 是 CMD 的高级版本,具有许多附加功能和特性。此外,它具有更好的图形界面和对服务器自动化任务的更好支持。Shell 的 Bash 也是如此。比较 Powershell 和 Bash 就像比较苹果和橙子一样,它们用于具有不同功能的不同操作系统。感谢您的阅读,我希望它能帮助到某些人。
RESOURCES
资源
- https://www.comparitech.com/net-admin/powershell-cheat-sheet/
- PowerShell 文档 - PowerShell | Microsoft Learn
https://docs.microsoft.com/en-us/powershell/ - Bash - GNU Project - Free Software Foundation
https://www.gnu.org/software/bash/ - Bourne shell - IBM Documentation
https://www.ibm.com/docs/en/aix/7.1?topic=shells-bourne-shell - What’s the Difference Between sh and Bash? | Baeldung on Linux
https://www.baeldung.com/linux/sh-vs-bash - https://www.javatpoint.com/powershell-vs-bash-shell
- Bash vs Shell: Difference and Comparison
https://askanydifference.com/difference-between-bash-and-shell/ - Difference Between CMD vs PowerShell vs Bash - AttuneOps
https://www.servertribe.com/difference-between-cmd-vs-powershell-vs-bash/
via:
-
Bash vs. PowerShell: Comparing Scripting Shells
https://smartscripter.com/bash-vs-powershell-choosing-the-right-scripting-shell/ -
Shell vs BASH vs PowerShell vs CMD | by A. Yigit Ogun | Medium
https://medium.com/@ayogun/shell-vs-bash-vs-powershell-vs-cmd-fa916895aab -
Difference Between CMD vs PowerShell vs Bash - AttuneOps
https://attuneops.io/difference-between-cmd-vs-powershell-vs-bash/ -
Evolution of shells in Linux (developerWorks)
https://lwn.net/Articles/471015/ -
PowerShell 与 Bash 相比,哪个更好? - 知乎
https://www.zhihu.com/question/25147047