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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou 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