相关疑难解决方法(0)

为什么 shell 命令替换会吞下尾随的换行符?

根据以下示例,以及我最近的问题 在 bash 中,尾随的换行符去哪儿了?,我想知道它“为什么”发生

  x="$(echo -ne "a\nb\n")" ; echo -n "$x" | xxd -p 
# Output is: 610a62 
# The trailing newline from the 'echo' command
#   has been "deleted" by Command Substitution
Run Code Online (Sandbox Code Playgroud)

我认为shell 操作必须有一些非常重要的原因,即命令替换,从它正在替换的命令输出中实际删除一些数据......
但我无法理解这个,因为它似乎是它该做什么的对立面......即。将命令的输出传递回脚本进程......保留一个字符对我来说似乎很奇怪,但我认为有一个合理的原因......我很想知道这个原因是什么...... .

shell text-processing command-substitution

27
推荐指数
3
解决办法
5915
查看次数

为什么带有命令替换 herestring 的 Bash 'while' 读取循环不读取整个输入?

看看这个非常基本的 Bash 脚本:

#!/bin/bash
while read l
do
  echo $l
  echo "next one"
done <<< $(ps aux)
Run Code Online (Sandbox Code Playgroud)

我的计算机上有多个进程,该ps aux命令在终端中运行良好。

我在循环中只得到一次迭代,ps命令输出在同一行给出。为什么它不能按预期工作?

bash command-substitution read here-string

3
推荐指数
1
解决办法
1389
查看次数