1

For example whoami and date. I can do this that way

whoami>/home/user/folder/file
date>>/home/user/folder/file

But i'm sure it can be done in one line without typing path two times. I have tried using

|

but always first command is ignored.

0

2 Answers 2

5

Use a subshell

(whoami; date) > ~user/directory/file
0
3

You could accomplish this with a single redirection using echo/printf along with command substitution:

printf '%s\n' "$(whoami)" "$(date)" > /home/user/folder/file

IMO this is not a good solution, it is not very readable/manageable...especially if more commands are added.

In order to prevent having to type the path multiple times you could just save it in a variable:

file=/home/user/folder/file
whoami>"$file"
date>>"$file"

But this requires multiple redirections as well.

If you wanted to write a long script or subset of commands and wanted to perform a single redirection for them you could use command grouping:

{
whoami
date
} > /home/user/folder/file

Or on one line:

{whoami;date;} > /home/user/folder/file

Not the answer you're looking for? Browse other questions tagged or ask your own question.