Skip to content
Advertisement

Is it possible to display a file’s contents and delete that file in the same command?

I’m trying to display the output of an AWS lambda that is being captured in a temporary text file, and I want to remove that file as I display its contents. Right now I’m doing:

... && cat output.json && rm output.json

Is there a clever way to combine those last two commands into one command? My goal is to make the full combined command string as short as possible.

Advertisement

Answer

For cases where

  • it is possible to control the name of the temporary text file.
  • If file is not used by other code

Possible to pass “/dev/stdout” as the.name of the output

Regarding portability: see stack exchange how portable … /dev/stdout

POSIX 7 says they are extensions. Base Definitions,

Section 2.1.1 Requirements: The system may provide non-standard extensions. These are features not required by POSIX.1-2008 and may include, but are not limited to: […]

• Additional character special files with special properties (for example,  /dev/stdin, /dev/stdout,  and  /dev/stderr)

Using the mandatory supported /dev/tty will force output into “current” terminal, making it impossible to pipe the output of the whole command into different program (or log file), or to use the program when there is no connected terminals (cron job, or other automation tools)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement