简体   繁体   English

试图 append (>>) 多条命令到一个文件中的一行

[英]Trying to append (>>) multiple commands to a file in one line

I'm trying to find a way to capture the stdout for two commands which are ran back to back in one line.我正在尝试找到一种方法来捕获两个命令的标准输出,这些命令在一行中背靠背运行。

For example if I have a blank file named "practice.txt":例如,如果我有一个名为“practice.txt”的空白文件:

echo Hello World; pwd >> practice.txt

I want the file to now contain:我希望文件现在包含:

Hello World
/home/sean

I'm not sure if this is already a frequently asked question or not, but I couldn't find anyone else asking after a quick search.我不确定这是否已经是一个常见问题,但我在快速搜索后找不到其他人问。 Any help would be appreciated.任何帮助,将不胜感激。

If you only want to run it once, consider a subshell如果您只想运行一次,请考虑使用 subshell

( echo "Hello World" ; pwd ) >> practice.txt

If you expect to run the collection several times (perhaps declaring something useful in your shell's .*rc file), you could put both calls into a function如果您希望多次运行该集合(可能在您的 shell 的.*rc文件中声明一些有用的内容),您可以将这两个调用放入 function

fn() {
    echo "Hello World"
    pwd
}

This can also be done on a single line这也可以在单行上完成

% fn() { echo "Hello World" ; pwd }
% fn >> practice.txt
% cat practice.txt
Hello World
/home/sean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM