Bash ( Bourne Again Shell ) 是命令行和图形用户界面,随所有Linux发行版一起分发。在 Windows 10 上使用Bash(Bash on Windows 10)并不是一个简单的过程。但是,Windows 11 包含一个升级的Windows子系统(Windows Subsystem)Linux ( WSL 2.0 ),让您可以比以往更轻松
地安装和使用Bash 。
新版本的WSL在虚拟机内运行真正的Linux 内核。(Linux kernel)这意味着您在WSL下运行的任何(WSL)Linux发行版都包含Bash。
如何在Windows 11上安装(Windows 11)WSL和Bash
要在Windows 11系统上安装和运行包含Bash的(Bash)Linux操作系统,您需要先安装WSL。在Windows 11上,这是使用Windows 终端(Windows Terminal)的简单过程。不要启动CMD(命令提示符(Command Prompt))—— Windows 终端(Windows Terminal)是一个不同的应用程序。
1. 选择开始(Start)按钮,然后在搜索字段中输入“终端”。在Windows 终端(Windows Terminal)窗格中,选择以管理员身份(Administrator)运行(Run)。
注意:如果Windows 终端(Windows Terminal)没有启动,您可能需要更新它。访问Microsoft Store并安装最新版本的 Windows 终端(latest version of the Windows Terminal)。
2. 在命令提示符处键入以下命令:wsl –install,然后按Enter。这条命令将下载并安装适用于Linux的最新版本的(Linux)Windows 子系统(Windows Subsystem)。下载为几百兆字节,因此安装过程可能需要一些时间才能完成。
3. 完成后,您应该会看到消息:“请求的操作成功。” 看到此消息后,重新启动系统以完成WSL安装。您可以在Windows 终端(Windows Terminal)中键入 shutdown /r /t 0以启动重新启动。
4. 系统重新启动后,该过程将继续安装Ubuntu作为默认Linux发行版。系统将提示您输入Linux系统的用户名和密码。
5. 完成后,Ubuntu将自动启动到Bash shell。您可以使用通常在Linux中使用的相同命令,包括将Linux应用程序安装到此虚拟环境中。
6.如果您愿意,可以安装其他 Linux 发行版。(other Linux distros)要查看哪些可以安装,请重新打开Windows 终端(Windows Terminal)( Powershell ),键入 wsl –list –online,然后按Enter。您将看到Opensuse、Debian等选项。
7. 您可以通过键入 wsl –install -d < 来安装这些发行版中的任何一个> 进入Windows 终端(Windows Terminal)。与之前的Ubuntu(Ubuntu)安装相同的过程将重复,您需要输入用户名和密码才能完成安装。
注意:您还可以从Microsoft Store在Windows上安装任何(Windows)Linux 发行版(Linux Distro)。
如何启动您的 Linux 发行版(Your Linux Distro)并使用 Bash(Use Bash)
您可以使用两种方法来启动Linux发行版。如果您安装了Ubuntu,您可以选择开始(Start)菜单,键入Ubuntu,然后选择Ubuntu应用程序来启动它。
或者,您可以启动Windows 终端(Windows Terminal)并键入命令Ubuntu以在(Ubuntu)Ubuntu环境
中启动Linux Bash shell 。
要查看可以在Bash(Bash)中使用的所有可用Linux命令的列表,请键入 help -d。
您可以通过键入 help 后跟命令名称来获取有关各个命令的更多详细信息。例如,help printf 返回命令详细信息和有关每个命令参数的信息。
大多数人在第一次开始使用时使用
的一些更常见的Bash命令包括:(Bash)
- 交互模式:在命令行界面( (Mode)Windows 终端(Windows Terminal))中
键入命令。
- 批处理模式(Mode):运行一个文本文件,其中包含您希望Linux按顺序执行的所有命令。许多人使用编程语法创建这些脚本。
如何在Windows中运行(Windows)Bash 脚本(Bash Script)
要在Bash(Bash)中运行脚本,只需使用您喜欢的文件编辑器(如记事本(Notepad))创建一个文本文件,并将其保存在您会记住的位置。
Bash脚本的独特之处在于第一行必须是“#!” 后跟Linux bash 路径的路径。要查看它是什么,请启动Ubuntu并在命令行窗口中键入 which bash。这将提供Bash路径。
创建一个新的文本文件并在顶部包含第一行。在本例中,这将是:
#!/用户/bin/bash
在此行后面添加您希望Linux运行的每个顺序命令。在这个例子中:
- 第 1 行:使用 echo 命令在屏幕上向用户显示文本。
- 第 2 行:将 echo 与 date 命令结合以返回今天的日期。
- 第 3 行:将 echo 与 whoami 命令结合以返回您的用户名。
使用 .sh 扩展名保存此文本文件。记住这个文件的路径。
接下来,打开Windows 终端(Windows Terminal)并使用 cd 命令导航到保存Bash脚本的路径。
要运行脚本,请键入 bash <<name of script>>。
这是一个非常简单的示例,但它演示了如何使用这样的文件来制作整个Bash脚本程序。甚至像 IF 语句这样的编程语句也可用于在脚本中包含决策功能。
站点SS64 提供了丰富的资源(SS64 provides an extensive resource),其中包含所有可用的Bash命令,您可以在Bash终端或Bash脚本中使用这些命令。
您还可以查看有关使用Linux shell 脚本备份目录(Linux shell script to back up a directory)的教程。
How to Install and Use Bash on Windows 11
Bash (Bourne Again Shell) is the command lіne, аnd graphical user interface diѕtributed with all Linux distributions. Using Bash on Windows 10 wasn’t a simple process. However, Windows 11 includes an upgraded Windows Subsystem for Linux (WSL 2.0) that allows you to install and use Bash easier than ever.
The new version of WSL runs a true Linux kernel inside of a virtual machine. This means that any Linux distribution you run under WSL includes Bash.
How to Install WSL and Bash on Windows 11
To install and run Linux operating systems that include Bash on your Windows 11 system, you’ll need to install WSL first. On Windows 11, this is a straightforward process using Windows Terminal. Do not launch CMD (Command Prompt) – Windows Terminal is a different application.
1. Select the Start button, and type “terminal” in the search field. In the Windows Terminal pane select Run as Administrator.
Note: If Windows Terminal doesn’t launch, you may need to update it. Visit the Microsoft Store and install the latest version of the Windows Terminal.
2. Type the following command: wsl –install, at the command prompt and press Enter. This single command will download and install the latest version of the Windows Subsystem for Linux. The download is a few hundred megabytes so the installation process may take some time to finish.
3. When it’s finished, you should see the message: “The requested operation is successful.” Once you see this message, restart your system to complete the WSL installation. You can type shutdown /r /t 0 in Windows Terminal to initiate the reboot.
4. After your system reboots, the process will continue by installing Ubuntu as the default Linux distro. You will be prompted to enter a username and password for the Linux system.
5. Once you’ve finished, Ubuntu will launch into the Bash shell automatically. You can use the same commands you would normally use in Linux, including installing Linux apps into this virtual environment.
6. You can install other Linux distros if you prefer. To see which ones are available to install, reopen Windows Terminal (Powershell), type wsl –list –online, and press Enter. You will see options like Opensuse, Debian, and others.
7. You can install any of these distros by typing wsl –install -d <<distro name>> into the Windows Terminal. The same process as the previous Ubuntu installation will repeat, and you’ll need to enter a username and password to complete the installation.
Note: You can also install any Linux Distro on Windows from the Microsoft Store.
How to Launch Your Linux Distro and Use Bash
There are two methods you can use to launch your Linux distro. If you installed Ubuntu, you can select the Start menu, type Ubuntu, and select the Ubuntu app to launch it.
Alternatively, you can launch the Windows Terminal and just type the command Ubuntu to launch the Linux Bash shell in the Ubuntu environment.
To see a list of all available Linux commands you can use in Bash, type help -d.
You can get more details about individual commands by typing help followed by the command name. For example, help printf returns command details and information about each command parameter.
Some of the more common Bash commands most people use when they first get started include:
- Interactive Mode: Type commands into the command-line interface (Windows Terminal).
- Batch Mode: Run a text file that contains all of the commands you’d like Linux to execute in order. Many people create these scripts using programming syntax.
How to Run a Bash Script in Windows
To run a script in Bash, just create a text file using your favorite file editor like Notepad and save it in a location you’ll remember.
One thing that’s unique about a Bash script is that the first line must be “#!” followed by the path of your Linux bash path. To see what that is, launch Ubuntu and type which bash into the command line window. This will provide the Bash path.
Create a new text file and include that first line at the top. In the case of this example, that would be:
#! /user/bin/bash
Follow this line with each sequential command you want Linux to run. In this example:
- Line 1: Uses the echo command to display text to the user on the screen.
- Line 2: Combines echo with the date command to return today’s date.
- Line 3: Combines echo with the whoami command to return your user name.
Save this text file with a .sh extension. Remember the path to this file.
Next, open the Windows Terminal and use the cd command to navigate to the path where you’ve saved the Bash script.
To run the script, type bash <<name of script>>.
This is a very simple example, but this demonstrates how you can use such a file to craft entire Bash script programs. Even programming statements like IF statements are available to include decision-making capabilities in your script.
The site SS64 provides an extensive resource with all available Bash commands that you can use in the Bash terminal or your Bash scripts.
You can also see a tutorial on using a Linux shell script to back up a directory.