Bash外壳

Bash is (as of today) the de facto shell on most systems you’ll get in touch with: Linux, macOS, and the WSL on Windows 10.

Bash是(迄今为止) 您将与之联系的大多数系统上的事实上的shell:Linux,macOS和Windows 10上的WSL。

Update: macOS since Catalina (fall 2019) uses Zsh

更新:自Catalina(2019年秋季)开始使用zsh的macOS

There are historical reasons that made Bash the most popular shell in the world. Back in 1989, when its was first released, the tech world was very different. At that time, most software in the UNIX world was closed source. Unix itself was proprietary and closed source.

有一些历史原因使Bash成为世界上最受欢迎的外壳。 早在1989年,当它第一次发布时,技术世界就大不一样了。 当时,UNIX世界中的大多数软件都是封闭源。 Unix本身是专有的并且是封闭源代码。

To use a UNIX system you had to use a shell.

要使用UNIX系统,必须使用外壳程序。

The most popular shell at the time was closed source and proprietary, you had to pay to use it. It was the Bourne shell, available under the /bin/sh command. It was called “Bourne” because its creator was Steve Bourne.

当时最流行的外壳是封闭源代码和专有的,您必须付费才能使用它。 它是Bourne外壳,在/bin/sh命令下可用。 之所以称为“伯恩”,是因为其创建者是史蒂夫·伯恩。

Richard Stallman in those years with the GNU Project (and later on Linux) was about to revolutionise everything, starting the Open Source revolution. The GNU Project needed a shell, and, with the help of the Free Software Foundation, Bash was born. Heavily inspired by the Bourne Shell, Bash means Bourne-again shell and it is one key ingredient of the GNU Project, and probably one of its most successful pieces of software that we still use today.

理查德·斯托曼(Richard Stallman)在那些年与GNU项目(后来在Linux上一起使用)即将彻底改变一切,开始了开源革命。 GNU项目需要一个外壳,在自由软件基金会的帮助下,Bash诞生了。 受Bourne Shell的大力启发,Bash 再次使用Bourne的含义,它是GNU工程的重要组成部分,并且可能是我们今天仍在使用的最成功的软件之一。

Bash could run all scripts written for sh, which was a mandatory feature for its adoption, and it also introduced many more features, since the very early days, offering a better experience to its users. Since those early days, Bash gained lots of improvements. This tutorial describes the most popular and useful things you can do with it.

Bash可以运行所有为sh编写的脚本,这是采用sh的强制功能,并且自从早期开始,它还引入了更多功能,从而为用户提供了更好的体验。 从早期开始,Bash取得了许多进步。 本教程描述了您可以使用的最受欢迎和最有用的功能。

Bash的第一步 (First steps in Bash)

Since Bash is the default shell in many systems, all you need to start a bash shell is to

由于Bash是许多系统中的默认shell,因此启动bash shell所需要做的只是

  • log in to the system, if it’s a server

    登录到系统(如果是服务器)
  • open your terminal, if it’s your computer

    打开终端(如果是您的计算机)

See my macOS terminal guide for more info on using your terminal on a Mac.

有关在Mac上使用终端的更多信息,请参阅我的macOS终端指南

As soon as you start it, you should see a prompt (which usually ends with $).

启动后,您应该会看到一个提示(通常以$结尾)。

How do you know the shell is running bash? Try typing help and pressing enter.

您怎么知道shell正在运行bash? 尝试键入help然后按Enter。

See? We just told Bash to execute the help command. This command in turns shows you the version of Bash you are running and a list of commands you can use.

看到? 我们只是告诉Bash执行help命令。 该命令依次显示正在运行的Bash的版本以及可以使用的命令列表。

Warning: see the version I have there? It’s 3.2.57. This is the default Bash version shipped in macOS, which does not include a higher release for licensing issues. This Bash version is from 2014. Install the latest Bash 5.x using Homebrew, by typing brew install bash.

警告:看到我那里的版本吗? 是3.2.57。 这是macOS随附的默认Bash版本,其中不包括用于许可问题的更高版本。 此Bash版本是2014年的版本。键入brew install bash ,使用Homebrew安装最新的Bash5.x。

More often than not you’ll never use any of the commands listed in the bash help, unless you are creating shell scripts or advanced things.

通常,除非创建外壳程序脚本或高级功能,否则您永远不会使用bash帮助中列出的任何命令。

99% of the day to day shell use is navigating through folders and executing programs like ls, cd and other commun UNIX utilities.

每天有99%的shell使用情况是浏览文件夹并执行lscd和其他公共UNIX实用程序之类的程序。

To navigate through the filesystem you will use the ls command. It’s available in the /bin/ls, and since Bash has the /bin folder in its paths list, you can just type ls to use it.

要浏览文件系统,您将使用ls命令。 它在/bin/ls可用,并且由于Bash在其路径列表中具有/bin文件夹,因此您只需键入ls即可使用它。

ls lists the files in the current folder. You usually start from your home folder, which depends on the system but on macOS is under /Users. My home folder is in /Users/flavio. This is not Bash related, it’s more of a UNIX filesystem thing, but arguments overlap and if you’ve never used a shell it’s good to know.

ls列出当前文件夹中的文件。 您通常从主文件夹开始,该文件夹取决于系统,但macOS在/Users 。 我的主文件夹位于/Users/flavio 。 这与Bash无关,更多的是UNIX文件系统,但是参数重叠,并且如果您从未使用过Shell,那么很高兴知道这一点。

To navigate to other folders you use the cd command, followed by the name of the folder you want to move to:

要导航到其他文件夹,请使用cd命令,后跟要移动到的文件夹的名称:

cd Documents

cd .. goes back to the parent folder.

cd ..返回到父文件夹。

Depending on your Bash configuration, you will see your current folder shown before the prompt (the $ symbol). Or you might not, but you can always know where you are by typing pwd and pressing enter.

根据您的Bash配置,您将看到当前文件夹显示在提示之前( $符号)。 也许您可能不知道,但是通过输入pwd并按Enter可以始终知道您的位置。

pwd means *p*athname of *w*orking *d*irectory

pwd表示* w * orking * d * irectory的* p * athname

命令行编辑 (Command line editing)

When you are writing your commands in the shell, notice that you can move left and right with the arrow keys. This is a shell feature. You can move around your commands, press the backspace button and correct commands. Pressing the enter key tells the shell to go and let the system execute the command.

在外壳中编写命令时,请注意,您可以使用箭头键左右移动。 这是一个外壳功能。 您可以四处移动命令,按退格按钮并更正命令。 按下enter键告诉外壳程序让系统执行命令。

This is normal and accepted behavior, but something that probably made “wow” the early UNIX users.

这是正常且可以接受的行为,但是这可能使早期的UNIX用户“惊叹”。

Keyboard combinations allow you to be quick at editing without reaching for the arrow keys:

键盘组合使您可以快速进行编辑,而无需触及箭头键:

  • ctrl+d to delete the currently selected character

    ctrl+d删除当前选择的字符

  • ctrl+f to go to the character on the right

    ctrl+f转到右侧的字符

  • ctrl+b to go to the character on the left

    ctrl+b转到左侧的字符

自动补全 (Autocompletion)

A nice feature of Bash when moving around the file system is autocompletion. Try typing cd Doc and press the tab key to make Bash autocomplete that to cd Documents. If there are multiple choices for those first characters Bash will return you the list, so you can type a couple more characters to help it remove the ambiguity and press tab again to complete.

Bash在文件系统中移动时的一个不错的功能是自动补全。 尝试键入cd Doc并按tab键使Bash自动完成对cd Documents 。 如果第一个字符有多个选择,Bash将返回您的列表,因此您可以输入更多字符以帮助消除歧义,然后再次按tab键以完成。

The shell can autocomplete file names, but also command names.

Shell可以自动完成文件名,也可以自动完成命令名。

Shell命令 (Shell commands)

Using the shell we can run commands available on the system. We can prefix the commands with the full path (e.g. /bin/ls to list files in a folder) but the shell has the concept of path so we can just type ls and it knows where most commands are found (and we can add folders to this path by configuring it).

使用外壳,我们可以运行系统上可用的命令。 我们可以在命令之前加上完整路径(例如/bin/ls来列出文件夹中的文件),但是外壳程序具有路径的概念,因此我们只需键入ls知道大多数命令的位置(我们可以添加文件夹)通过配置此路径)。

Commands accept arguments. For example ls /bin will list all files in the /bin folder.

命令接受参数。 例如, ls /bin将列出/bin文件夹中的所有文件。

Parameters are prefixed by a dash -, like ls -a which tells ls to also show hidden files. Hidden files as a convention are files (and folders) starting with a dot (.).

参数以短划线-开头,例如ls -a ,它告诉ls也显示隐藏文件。 按照惯例,隐藏文件是指以点( . )开头的文件(和文件夹)。

常见的Shell命令 (Common shell commands)

There are a lot of commands preinstalled on any system, and they vary considerably depending if it’s Linux / macOS or even the Linux distribution you are running.

在任何系统上都预装了很多命令,它们的差异很大,具体取决于运行的是Linux / macOS还是Linux发行版。

However let’s do a brief roundup of the most common shell commands you can run. Those are not provided by shells per se, but rather they are command line commands you can invoke through shells.

但是,让我们对您可以运行的最常见的shell命令进行简要汇总。 Shell本身并不提供这些命令,而是可以通过Shell调用的命令行命令。

Any time you have problems, like you don’t know what a command does, or you don’t know how to use it, use man. It lets you get the help for all of the commands I will list, and more. Run man ls for example.

任何时候遇到问题(例如您不知道命令的功能或不知道如何使用命令),请使用man 。 它使您可以获得我将列出的所有命令的帮助,以及更多。 例如运行man ls

These are filesystem commands:

这些是文件系统命令:

  • ls to list files

    ls列出文件

  • cd to change folder

    cd更改文件夹

  • rm to remove a file or folder

    rm删除文件或文件夹

  • mv to move a file to another folder, or change a file name

    mv将文件移动到另一个文件夹,或更改文件名

  • cp to copy a file

    cp复制文件

  • pwd to show the current working directory

    pwd显示当前工作目录

  • mkdir to create a folder

    mkdir创建一个文件夹

Every file on a Unix filesystem has permissions. chmod allows you to change those (not going into it now), and chown allows you to change the file owner.

Unix文件系统上的每个文件都有权限。 chmod允许您更改这些内容(现在不进行介绍),而chown允许您更改文件所有者

cat, tail and grep are two super useful commands to work with files.

cattailgrep是使用文件的两个超级有用的命令。

pico, nano, vim and emacs are commonly installed editors.

piconanovimemacs是常用的编辑器。

whereis on macOS shows where a command is located on the system.

macOS上的whereis显示了命令在系统上的位置。

There are many more commands, of course, but those are a few you might run into more often.

当然,还有更多命令,但是您可能会经常遇到这些命令。

执行命令 (Executing commands)

ls and cd are commands, as I mentioned, which are found in the /bin folder. You can execute any file, as long as it’s an executable file, by typing its full path, for example /bin/pwd. Commands don’t need to be in the /bin folder, and you can run executable files that are in your current folder using the ./ path indicator.

正如我提到的, lscd是在/bin文件夹中找到的命令。 只要是可执行文件,就可以通过键入其完整路径来执行任何文件,例如/bin/pwd 。 命令不需要位于/bin文件夹中,并且您可以使用./ path指示符运行当前文件夹中的可执行文件。

For example if you have a runme file in your /Users/flavio/scripts folder, you can run

例如,如果您的/Users/flavio/scripts文件夹中有一个runme文件,则可以运行

cd /Users/flavio/scripts

and then run ./runme to run it.

然后运行./runme来运行它。

Or you could run /Users/flavio/scripts/runme from Bash, wherever your current folder is.

或者,您可以从Bash运行/Users/flavio/scripts/runme ,无论当前文件夹在哪里。

工作 (Jobs)

Whenever you run a command, if it’s a long running program, you will have your shell completely owned by that command. You can kill the command using ctrl-C .

每当您运行命令时(如果它是一个运行时间较长的程序),您的外壳将完全归该命令所有。 您可以使用ctrl-C命令。

At any time you can run jobs to see the jobs you are running, and their state.

您可以随时运行jobs以查看正在运行的作业及其状态。

$ jobs
Job	Group	State	Command
1	72292	stopped	ftp

Another useful command is ps which lists the processes running.

另一个有用的命令是ps ,它列出了正在运行的进程。

$ ps
  PID TTY           TIME CMD
19808 ttys000    0:00.56 /usr/local/bin/fish -l
65183 ttys001    0:04.34 -fish
72292 ttys001    0:00.01 ftp

top shows the processes and the resources they are consuming on your system.

top显示了进程及其在系统上消耗的资源。

A job or process can be killed using kill <PID>.

可以使用kill <PID>作业或进程。

命令历史 (Command history)

Pressing the up arrow key will show you the history of the commands you entered. Again, this is a shell feature. Pressing the down arrow key will let you navigate back and forth in time to see what commands you entered previously, and pressing enter will let you run that command again.

up箭头键将向您显示您输入的命令的历史记录。 同样,这是一个外壳功能。 按down箭头键可让您及时前后浏览以查看先前enter命令,按enter可让您再次运行该命令。

This is a quick access to the command history. Running the history command will show you all the commands entered in the shell:

这是对命令历史记录的快速访问。 运行history命令将显示在shell中输入的所有命令:

When you start typing a command, Bash can autocomplete it by referencing a previously entered command in the history. Try it by pressing esc followed by tab.

当您开始键入命令时,Bash可以通过在历史记录中引用先前输入的命令来自动完成命令。 按esc然后按tab尝试。

To be honest I find the Fish shell implementation of this to be much better and easier

老实说,我发现此方法的Fish shell实现会更好,更容易

设置默认外壳 (Setting your default shell)

There are many more shells other than Bash. You have Fish, ZSH, TCSH, and others. Any user on the system can choose its own shell.

除了Bash之外,还有更多其他的Shell。 您有Fish,ZSH,TCSH等。 系统上的任何用户都可以选择自己的外壳。

You set your default login shell by running the chsh -s chsh -s /bin/bash command. You’ll almost never need to do that, unless it was previously changed, for example with Fish:

您可以通过运行chsh -s chsh -s /bin/bash命令来设置默认登录Shell。 除非以前进行了更改(例如使用Fish进行更改),否则您几乎不需要这样做。

chsh -s /usr/local/bin/fish

自定义Bash (Customizing Bash)

I noted before that you might (or might not) see your current working directory in the Bash prompt. Where is this determined? In the Bash configuration!

我之前曾指出,您可能会(也可能不会)在Bash提示符下看到当前的工作目录。 在哪里确定? 在Bash配置中!

There’s a bit of confusion here because Bash uses a different configuration file for different scenarios, and it also reads multiple configuration files.

这里有些混乱,因为Bash在不同情况下使用不同的配置文件,并且它还会读取多个配置文件。

Let’s give some order to this confusion. First, there’s a big distinction whether Bash is initialized as a login shell or not. By login shell we mean that the system is not running a GUI (Graphical User Interface) and you log in to the system through the shell. That’s the case of servers, for example.

让我们对这种混乱给予一些命令。 首先,是否将Bash初始化为登录shell有很大的区别。 登录外壳程序是指系统未运行GUI(图形用户界面),而您是通过外壳程序登录到系统的。 例如,服务器就是这种情况。

In this case, Bash loads this configuration file:

在这种情况下,Bash将加载此配置文件:

/etc/profile

and then looks in the user home folder and looks for one of these files, in order, executing the first one it finds:

然后在用户主文件夹中查找并依次查找以下文件之一,执行发现的第一个文件:

~/.bash_profile
~/.bash_login
~/.profile

were ~ means your home folder (it’s automatically translated by Bash)

~表示您的主文件夹(由Bash自动翻译)

This means that if there’s a .bash_profile, ~/.bash_login and ~/.profile are never run, unless explicitly executed in .bash_profile.

这意味着,如果存在.bash_profile ,则除非在.bash_profile明确执行,否则从不运行~/.bash_login~/.profile

If instead of being a login shell Bash is run like I do with macOS, for example, as a normal application, the configuration files change. Bash loads first /etc/bash.bashrc, then ~/.bashrc.

例如,如果像普通的应用程序那样运行Bash而不是像我对macOS一样运行Bash,则配置文件会更改。 Bash首先加载/etc/bash.bashrc ,然后加载~/.bashrc

环境变量 (Environment variables)

Sometimes you have programs that use environment variables. Those are values that you can set outside of the program, and alter the execution of the program itself. An API key, for example. Or the name of a file.

有时您有使用环境变量的程序。 您可以在程序外部设置这些值,并更改程序本身的执行。 例如,API密钥。 或文件名。

You can set an environment variable using the syntax

您可以使用以下语法设置环境变量

VARIABLE_NAME=variable_value

The value can contain white spaces, by using quotes

通过使用引号,该值可以包含空格

VARIABLE_NAME="variable value"

A bash script can use this value by prepending a dollar sign: $VARIABLE_NAME.

bash脚本可以通过在美元符号前加上$VARIABLE_NAME来使用此值。

Also other programming languages commands can use environment variables, for example here’s how to read environment variables with Node.js.

另外,其他编程语言命令也可以使用环境变量,例如,这是使用Node.js读取环境变量的方法

The system sets up some environment variables for you, like

系统会为您设置一些环境变量,例如

  • $HOME your home folder

    $HOME您的主文件夹

  • $LOGNAME your user name

    $LOGNAME您的用户名

  • $SHELL the path to your default shell

    $SHELL默认外壳程序的路径

  • $PATH the path where the shell looks for commands

    $PATH shell在其中查找命令的路径

You can inspect their value by prepending echo to them:

您可以通过在它们前面添加echo来检查它们的值:

echo $LOGNAME # flavio
echo $HOME # /Users/flavio

特殊的环境变量:$ PATH (A special environment variable: $PATH)

I mentioned the $PATH variable. This is a list of folders where the shell will look into when you type a command. Folders are separated by a colon : and they are written in order - Bash will look into the first, search for the command you asked for, and run it if it finds it. Otherwise if goes to the next folder and so on.

我提到了$ PATH变量。 这是当您键入命令时外壳将在其中查找文件夹的列表。 文件夹之间用冒号分隔:并按顺序编写-Bash将查找第一个文件夹,搜索所需的命令,并在找到后运行该命令。 否则,请转到下一个文件夹,依此类推。

My path is currently:

我的路径当前是:

bash-5.0$ echo $PATH
/usr/local/bin:/usr/bin: /bin:/usr/sbin: /sbin:/usr/local/go/bin

You typically edit this in the ~/.bashrc file by prepending or appending items:

通常,您可以在~/.bashrc文件中通过在项目之前或之后添加内容来对其进行编辑:

PATH = "$PATH:/Users/flavio/bin"

别名 (Aliases)

Using aliases we can set up shortcuts for common commands. You can give a quick name to a complex combination of parameters, for example.

使用别名,我们可以为常用命令设置快捷方式。 例如,您可以给复杂的参数组合起一个快速的名字。

You define an alias using the syntax

您使用语法定义别名

alias <alias>=<command>

if there’s a space in the command you use quotes:

如果命令中有空格,请使用引号:

alias <alias>="<command>"

One alias I commonly add to my system is ll:

我通常添加到系统中的一个别名是ll

alias ll="ls --al"

You normally define aliases in your ~/.bashrc file.

您通常在~/.bashrc文件中定义别名。

Just be careful with quotes if you have variables in the command: using double quotes the variable is resolved at definition time, using single quotes it’s resolved at invokation time. Those 2 are different:

如果命令中有变量,请小心使用引号:使用双引号将变量在定义时解析,使用单引号将在调用时解析。 那两个是不同的:

alias lsthis="ls $PWD"
alias lscurrent='ls $PWD'

$PWD refers to the current folder the shell is into. If you now navigate away to a new folder, lscurrent lists the files in the new folder, lsthis still lists the files in the folder you were when you defined the alias.

$ PWD是指外壳程序所在的当前文件夹。 如果现在导航到新文件夹,则lscurrent列出新文件夹中的文件,而lsthis仍会列出您在定义别名时所在的文件夹中的文件。

先进的命令行功能 (Advanced command line features)

通配符 (Wildcards)

ls and many other commands can make great use of wildcards. You can list all files starting with image:

ls和许多其他命令可以充分利用通配符。 您可以列出所有以image 开头的文件:

ls image*

Or all files ending with image:

或所有以image 结尾的文件:

ls *image

or all files that contain image inside the name:

或名称中包含image的所有文件:

ls *image*

重定向输出和标准错误错误 (Redirecting output and standard error error)

By default, commands started in the shell print out both the output and errors back to the shell. This might not be what you want. You can decide to write the output to a file instead.

默认情况下,在外壳程序中启动的命令将输出和错误都打印回外壳程序。 这可能不是您想要的。 您可以决定将输出写入文件。

Actually, to a /different file/, because in Unix even the screen is considered to be a file. In particular,

实际上,对于/ different file /,因为在Unix中,甚至屏幕也被视为文件。 特别是,

  • 0 identifies the standard input

    0标识标准输入

  • 1 identifies the standard output

    1标识标准输出

  • 2 identifies the standard error

    2确定标准误差

You can redirect the standard output to a file by appending 1> after a command, followed by a file name.

您可以通过在命令后附加1>后跟文件名来将标准输出重定向到文件。

Using the same technique you can use 2> to redirect the standard error.

使用相同的技术,您可以使用2>重定向标准错误。

There is a shortcut > for 1>, since that is used quite a lot.

有一个快捷方式> 1> ,因为使用了很多。

Example:

例:

ls 1> list.txt 2> error.txt
ls > list.txt 2> error.txt

Another shortcut, &>, redirects /both/ standard output and standard error to a file.

另一个快捷方式&>将/ both /标准输出和标准错误重定向到文件。

ls &> output.txt

Another frequent thing is to redirect standard error to standard output using 2>&1.

另一个常见的事情是使用2>&1将标准错误重定向到标准输出。

在后台运行命令 (Running a command in the background)

You can tell Bash to run a program in the background without it taking control of the shell, by appending & after it:

您可以通过在&之后添加&来告诉Bash在后台运行程序而无需控制外壳程序:

top &

top is a command that lists the processes running, ordered by most resource consuming.

top是一个命令,该命令列出了正在运行的进程,按消耗大量资源的顺序排列。

The application that would normally get control of the shell, is now started but nothing seems to happen. You can bring it back into focus by typing fg (aka *f*ore*g*round), but now we’re entering in the realm of processes and jobs which is a big topic on its own, but a quick overview:

现在可以启动通常可以控制外壳的应用程序,但是似乎什么也没发生。 您可以通过输入fg (又名* f * ore * g * round)使其重新成为焦点,但是现在我们进入了流程和作业领域,这本身就是一个很大的话题,但是快速概述一下:

When a command is running you can use ctrl-Z to pause it and bring it to the background. The shell comes back again in the foreground, and you can now run bg to move resume execution of that previously paused job.

运行命令时,可以使用ctrl-Z暂停它并将其带到后台。 Shell再次在前台返回,您现在可以运行bg来移动先前暂停的作业的恢复执行。

When you are ready to go back to it, run fg to bring back that program in the foreground.

当您准备好返回它时,请运行fg将该程序带回到前台。

You can see all processes running using ps, and the list shows all the processes pid numbers. Using the paused process pid, you can bring to foreground a specific command, for example fg 72292. Same works for bg.

您可以使用ps查看所有正在运行的进程,该列表显示所有进程的pid号。 使用暂停的进程pid ,可以将特定命令(例如fg 72292 。 同样适用于bg

排队命令 (Queuing commands)

You can instruct Bash to run a command right after another ends by separating them with a semicolon:

您可以通过用分号将Bash分开来指示Bash立即运行命令:

cd /bin; ls

You can repeat this to queue multiple commands in the same line.

您可以重复此操作以在同一行中将多个命令排队。

输出重定向 (Output redirection)

A program can receive input from any file using the < operator, and save to a file the output using the > operator:

程序可以使用<运算符从任何文件接收输入,并使用>运算符将输出保存到文件中:

echo hello > result.txt
wc < result.txt

wc is a command that counts the words it receives as input.

wc是一个命令,它计算输入的单词数。

(Pipe)

Using pipes, any command output can be used as input for a second command. Use the | operator to combine the two. In this example, wc gets its input from the output of echo hello:

使用管道,任何命令输出都可以用作第二个命令的输入。 使用| 运算符将两者结合起来。 在此示例中, wcecho hello的输出获取其输入:

echo hello | wc

分组命令 (Grouping commands)

Use && to combine two commands using “and”. If the first command executes without problems, run the second, and so on.

使用&&结合使用“ and”的两个命令。 如果第一个命令执行没有问题,请运行第二个,依此类推。

Use || to combine two commands using “or”. If the first command executes without problems the second does not run.

使用|| 使用“或”组合两个命令。 如果第一个命令执行没有问题,第二个命令则不会运行。

! negates the next logical operation:

! 否定下一个逻辑运算:

$ echo hello && echo test
hello
test
$ echo hello || echo test
hello
$ ! echo hello || echo test
hello
test

You can use parentheses to combine expressions to avoid confusion, and also to change the precedence:

您可以使用括号来组合表达式,以避免混淆,也可以更改优先级:

$ ! (echo hello) || (echo test)
hello
test
$ ! (echo hello || echo test)
hello

编程外壳 (Programming shells)

One of the best features of a shell like Bash is its ability to create programs with it, by basically automating commands execution.

像Bash这样的shell的最佳功能之一是能够通过基本自动化命令执行来使用它创建程序的能力。

I’m going to write a separate guide on Bash scripting soon, which will be separate from this tutorial because the topic is really more in-depth than what I want to add in this introductory Bash guide.

我很快将要编写一份有关Bash脚本的单独指南,该指南将与本教程分开,因为该主题确实比我希望在此Bash入门指南中添加的内容更深入。

A quick intro though: a script is a text file that starts with a line that indicates that it’s a shell script (and what shell it requires), followed by a list of commands, one per line. Example:

一个快速的介绍虽然:脚本是一个文本文件,以表示它是一个shell脚本行开始( 什么壳它需要),其次是命令,每行一个列表。 例:

#!/bin/bash
ls

You can save this as a file myscript and make it executable using chmod +x myscript, and run it using ./myscript (./ means “current folder”).

您可以将此保存为文件myscript ,并使其可执行使用chmod +x myscript ,并使用运行./myscript ( ./意思是“当前文件夹”)。

Shell scripting is outside of the scope of this post, but I want you to know about this. Scripts can have control structures and many other cool things.

Shell脚本编写不在本文讨论范围之内,但是我希望您对此有所了解。 脚本可以具有控制结构和许多其他很酷的东西。

This same scripting strategy works for other shells, like Zsh:

同样的脚本策略也适用于其他Shell,例如Zsh:

#!/bin/zsh
ls

翻译自: https://flaviocopes.com/bash/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值