linux - 将命令输出保存到变量而不打印到标准输出

标签 linux bash

我正在执行一个 bash 脚本,我正在获取此命令的输出:

fpings=$(fping -c 1 -t 1 $ips | sort) 

fpings 变量确实包含命令的输出,并且命令的实际输出不会打印到 shell,但它仍然会为每个 ip ping 向 shell 写入一行。

有一个开关 (-q) 可以抑制输出(我想要的部分),但没有任何开关可以抑制我不想要的部分。

如何在不向 shell 打印内容的情况下从 fpings 命令获取结果?

最佳答案

如果你不想看到标准错误,将它重定向到/dev/null:

fpings=$(fping -c 1 -t 1 $ips 2>/dev/null | sort) 

关于linux - 将命令输出保存到变量而不打印到标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17603823/

相关文章:

linux - 在路径中插入变量(bash 命令行)

bash - 重命名多个目录,同时保留中间部分具有不同的后缀

bash - xargs 或 tail 给出错误,目录名称中有空格

linux - 设置 HLS 分段时间

linux - Bash - 获取进程 pid 并逐行解析输出

linux - sh : 1: : Permission denied when launching buildscript

linux - 在 Windows 上为 Linux 编程

linux - 将历史从某一行兑换到另一行

linux - 移动或删除文件时使 `tail -f` 退出的 bash 脚本

XMLStarlet 不删除元素