Some key features of the bash shell

640?wxfrom=5&wx_lazy=1


         The bash shell is one of several shells available for Linux, also known as the Bourne-again shell, named after Stephen Bourne, the creator of an early shell (/bin/sh). Bash is highly compatible with sh, but it offers many improvements in both functional and programming capabilities. It combines features from the Korn shell (ksh) and the C shell (csh) in an attempt to be a POSIX compliant shell.


         Before diving into bash, recall that a shell is a program that accepts and executes commands. It also supports programming constructs that allow building complex commands with smaller parts. These complex commands or scripts can be saved as files and become new commands on their own. In fact, many commands on a typical Linux system are scripts.


         Shell has built-in commands like cd, break, and exec. Other commands are external commands.


Shell also uses 3 standard I/O streams


stdin is the standard input stream, which provides input to the command.

stdout is the standard output stream, which displays the output from the command.

stderr is the standard error stream, which displays error output from commands.


         Input streams provide input to programs, usually from terminal keystrokes. The output stream prints text characters, usually to a terminal. Terminals were originally ASCII typewriters or display terminals, but are now usually windows on a graphical desktop. More details on how to redirect these standard I/O streams will be covered in another tutorial in this series.


commands and sequences


         Its main function is to interpret your commands so that you can interact with the Linux system. On Linux (and UNIX®) systems, commands have a command name and then have options and arguments. Some commands have neither options nor parameters, and some commands have only one of them.


         If a line of code contains a # character, all remaining characters on that line are ignored. So the # character can represent both a comment and a root prompt. It's obvious from the context what it's supposed to mean.


Echo


The echo command prints (or echoes) its arguments to the terminal as shown below.

Echo example

[root@opsexm ~]$ echo Word

Word

[root@opsexm ~]$ echo A phrase

A phrase

[root@opsexm ~]$ echo Where are my spaces?

Where are my spaces?

[root@opsexm ~]$ echo "Here are my spaces." # plus comment

Here are my spaces.


         In the 3rd example above, all extra spaces are compressed into a single space in the output. To avoid this, use double quotes (") or single quotes (') to quote strings. Bash uses whitespace (such as spaces, tabs, and newlines) to split input lines into tokens, Then pass to the command. Quoting the string preserves extra whitespace, making the entire string a token. In the example above, each token after the command name is an argument, so we have 1, 2 respectively , 4 and 1 parameters.


         The echo command has two options. Normally, echo appends a newline to the end of the output. This behavior can be suppressed with the -n option. Certain backslash-escaped characters can be given special meaning using the -e option. Some characters are shown in the list below.


Echo and escape characters


Escape character sequence function

\a alarm(alarm)

\b backspace

\c suppress trailing newlines (equivalent to -n option)

\f form feed (clears screen on video monitor)

\n newline

\r Enter

\t horizontal tab


Escape and line continuation


         There is a small problem with using backslashes in bash. When a backslash character (\) is quoted, it acts as an escape character to tell bash itself to preserve the literal meaning of the following character. This is necessary for special shell metacharacters, which we'll cover later. There is one exception to this rule: a backslash followed by a newline causes bash to merge the two characters, treating the sequence as a continuation request. This is handy for splitting long lines, especially in shell scripts.


         要让 echo命令或其他许多使用类似转义控制字符的命令正确处理上述序列,必须将转义序列放在引号中,或者作为引号中的字符串的一部分,除非您使用了第二个反斜杠让 shell 将其中一个保留给命令。下面例子给出了 \ 的各种用法的一些示例。


更多 echo 示例

[root@opsexm ~]$ echo -e "No new line\c"

No new line[root@opsexm ~]$ echo "A line with a typed

> return"

A line with a typed

return

[root@opsexm ~]$ echo -e "A line with an escaped\nreturn"

A line with an escaped

return

[root@opsexm ~]$ echo "A line with an escaped\nreturn but no -e option"

A line with an escaped\nreturn but no -e option

[root@opsexm ~]$ echo -e Doubly escaped\\n\\tmetacharacters

Doubly escaped

metacharacters

[root@opsexm ~]$ echo Backslash \

> followed by newline \

> serves as line continuation.

Backslash followed by newline serves as line continuation.


         请注意,当您键入的一行中包含不匹配的引号时,bash 会显示一个特殊提示符 (>)。您的输入字符串会延续到第二行,并包含换行符。


Bash shell 元字符和控制运算符


         Bash 有多个 元字符,在未加引号时,它们的作用是将输入分解为单词。除了空格之外,元字符还包括:

|

&

;

(

)

<

>

         我们将在本教程的其他部分更详细讨论其中一些元字符。但现在需要注意的是,如果想要包含元字符作为文本的一部分,则必须将它放在引号中或使用反斜杠 (\) 转义,如 下方例子中所示。

换行符和某些元字符或元字符对也可以用作 控制运算符。它们是:

||

&&

&

;

;;

|

(

)

这些控制运算符中的部分运算符可用于创建命令 序列或 列表。


         最简单的命令序列是由一个分号 (;) 分开的两个命令。每个命令按顺序执行。在任何可编程环境中,命令都会返回成功或失败指示;Linux 命令通常返回 0 值表示成功,在失败时返回非 0 值。可以使用 && 和 || 控制运算符向列表中引入一些条件处理。如果使用控制运算符 && 将两个命令分开,当且仅当第一个命令返回退出值 0 时,才会执行第二个命令。如果使用 || 分离这些命令,那么只在第一个命令返回非 0 的退出代码时,才会执行第二个命令。清单 5给出了一些使用 echo 命令的命令序列。这些命令序列不是很有趣,因为 scho 返回了 0,但是在后面有更多命令可用时,您会看到更多例子。


命令序列


[root@opsexm ~]$ echo line 1;echo line 2; echo line 3

line 1

line 2

line 3

[root@opsexm ~]$ echo line 1&&echo line 2&&echo line 3

line 1

line 2

line 3

[root@opsexm ~]$ echo line 1||echo line 2; echo line 3

line 1

line 3

Exit


         可以使用 exit命令终止 shell。可以提供一个退出代码作为参数,此操作是可选的。如果在图形桌面上的终端窗口中运行 shell,您的窗口将会关闭。类似地,如果使用 ssh 或 telnet(举例而言)连接到远程系统,您的连接将终止。在 bash shell 中,还可以按住 Ctrl键并按下 d键来退出。


         让我们看看另一个控制运算符。如果将一个命令或命令列表放在括号中,该命令或序列会在一个子 shell 中执行,所以 exit 命令会退出子 shell,而不是退出您所在的 shell。下方例子给出了一个结合使用 &&、|| 和两个不同的退出代码的示例。


子 shell 和序列


[root@opsexm ~]$ (echo In subshell; exit 0) && echo OK || echo Bad exit

In subshell

OK

[root@opsexm ~]$ (echo In subshell; exit 4) && echo OK || echo Bad exit

In subshell

Bad exit


Linux云计算5月免费课程火热抢先中5天免费课程抢先听,运维干货内容免费听点击文末“阅读原文”即可免费听运维课火速抢先~~~~

PS:记得查收小编送你的免费大礼包呦~

福利 | 一万多套PPT模板等你免费来拿!无条件领取!

免费送 | 1000多套简历模板免费拿,附赠简历制作教程!

免费领 | 《Shell脚本 100例》电子书免费拿,运维必备干货~

640? 640?wx_fmt=jpeg

▼▼Click [ read the original text ] to listen to the 5-day Linux operation and maintenance dry goods sharing class for free , the hot lecture is in progress, come and grab it!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326007557&siteId=291194637