0

Is there an elegant way to both echo a command and save it to a file? The common use case is echo "test" >> someappendedfile.txt, in which case the command does not appear in bin/bash. By definition, >> is a re-direction, so doing both seems like a weird command.

I would like the result of something like echo "test" >> someappendedfile.txt to both log to stdout and the appending file.

Is there an alternative operator or utility that does what I am asking?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
9301293
  • 514
  • 3
  • 16
  • 4
    FYI -- `echo "foo" >> somefile` is really inefficient if you put `>> somefile` on every line over and over; it re-opens the output every single time you want to run a command and closes it again after. Much more efficient to do your redirection just once: `exec >> somefile` at the top of your script -- or, for the case at hand, `exec > >(tee -a somefile)` to redirect to `tee` (and thus to copy all content *both* to the file and to the original stdout location). – Charles Duffy Jul 31 '17 at 21:15
  • @CharlesDuffy I think that tee was what I was looking for! Is there a way to change the question title of one of the other posts? The reason I couldn't find the help I was looking for was that I had no idea tee existed! – 9301293 Jul 31 '17 at 21:16
  • 2
    That's the point of marking a question duplicate rather than deleting it -- that way other folks can use it as a guidepost to find preexisting questions that are on-point for them, but where they weren't using the right search terms. – Charles Duffy Jul 31 '17 at 21:17
  • @CharlesDuffy Thanks! – 9301293 Jul 31 '17 at 21:18

0 Answers0