简体   繁体   中英

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":

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

( 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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