为什么bash的吞咽-e在阵列前端 [英] Why is bash swallowing -e in the front of an array

查看:161
本文介绍了为什么bash的吞咽-e在阵列前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于语法如下:

  X =( -  2);回声$ {X [@]}; X =( - 电子2 -e);回声$ {X [@]}

输出:

  -a 2
2 -e

所需的输出

  -a 2
-e 2 -e

这是怎么回事?如何解决?


解决方案

TL;博士

 的printf%S \\ n$ {X [*]}

说明

回声需要3个选项:

  $帮助回声
[...]
选项​​:
  -n不追加新行
  -e启用以下反斜杠国米pretation
  反斜杠逃逸-e明确燮preSS间pretation

所以,如果你运行:

  $回声-n
$回声-n -e
$回声-n -e -E

您什么也没得到。即使你把引号中的每一个选项,它仍然看起来同样来砸:

  $回声-n
$回声-n-e

最后一个命令运行回声有两个参数: -n -e 。现在对比,与:

  $回声-n -e
-n -e

我们所做的是运行回声一个参数 -n -e 。由于bash中无法识别(组合)选项 -n -e ,它终于呼应单个参数到终端像我们想要的。

应用到阵列

在第二种情况下,数组 X 开始元素 -e 。之后bash扩展阵列 $ {X [@]} ,您实际上运行:

  $回声-e2-e
2 -e

由于第一个参数 -e ,这是PTED作为一个选项(而不是回显到终端),因为我们已经看到了间$ P $。

现在对比,与阵列扩展的其他样式 $ {X [*]} ,从而有效地执行以下操作:

  $回声-e 2 -e
-e 2 -e

bash中看到一个参数 -e 2 -e - 而且,因为它不承认作为一个选项 - 它相呼应的参数,终端

注意 $ {X [*]} 风格扩张的是不是一般的安全即可。看看下面的例子:

  $ X =( -  E)
$回声$ {X [*]}

尽管我们预计什么也没有打印 -e 来呼应。如果你一直在关注,你已经知道为什么是这样的情况。

摆脱

解决的办法是逃避任何参数的回声命令。不幸的是,不像提供一些方式说:嘿其它命令!以下参数的是不是要PTED作为一个选项跨$ P $(通常是 - 参数),Bash提供没有这样的转义机制为回声

幸运的是的printf 命令,它提供的是回声提供功能的超集。因此,我们的解决办法:

 的printf%S \\ n$ {X [*]}

Given the following syntax:

x=(-a 2);echo "${x[@]}";x=(-e 2 -e); echo "${x[@]}"

Output:

-a 2
2 -e

Desired output

-a 2
-e 2 -e

Why is this happening? How do I fix?

解决方案

tl;dr

printf "%s\n" "${x[*]}"

Explanation

echo takes 3 options:

$ help echo
[…]
Options:
  -n    do not append a newline
  -e    enable interpretation of the following backslash escapes
  -E    explicitly suppress interpretation of backslash escapes

So if you run:

$ echo -n
$ echo -n -e
$ echo -n -e -E

You get nothing. Even if you put each option in quotes, it still looks the same to bash:

$ echo "-n"
$ echo "-n" "-e"

The last command runs echo with two arguments: -n and -e. Now contrast that with:

$ echo "-n -e"
-n -e

What we did was run echo with a single argument: -n -e. Since bash does not recognize the (combined) option -n -e, it finally echoes the single argument to the terminal like we want.

Applied to Arrays

In the second case, the array x begins with the element -e. After bash expands the array ${x[@]}, you are effectively running:

$ echo "-e" "2" "-e"
2 -e

Since the first argument is -e, it is interpreted as an option (instead of echoed to the terminal), as we already saw.

Now contrast that with the other style of array expansion ${x[*]}, which effectively does the following:

$ echo "-e 2 -e"
-e 2 -e

bash sees the single argument -e 2 -e — and since it does not recognize that as an option — it echoes the argument to the terminal.

Note that ${x[*]} style expansion is not safe in general. Take the following example:

$ x=(-e)
$ echo "${x[*]}"

Nothing is printed even though we expected -e to be echoed. If you've been paying attention, you already know why this is the case.

Escaping

The solution is to escape any arguments to the echo command. Unfortunately, unlike other commands which offer some way to say, "hey! the following argument is not to be interpreted as an option" (typically a -- argument), bash provides no such escaping mechanism for echo.

Fortunately there is the printf command, which provides a superset of the functionality that echo offers. Hence we arrive at the solution:

printf "%s\n" "${x[*]}"

这篇关于为什么bash的吞咽-e在阵列前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆