Create temporary file and redirect output to it in one command Create temporary file and redirect output to it in one command shell shell

Create temporary file and redirect output to it in one command


If you're comfortable with the side effect of making the assignment conditional on tempfile not previously having a nonempty value, this is straightforward via the ${var:=value} expansion:

cat "$HOME/.bash_history" >"${tempfile:=$(mktemp)}"


I guess there is more than one way to do it. I found following to be working for me:

cat myfile.txt > $(echo "$(mktemp)")

Also don't forget about tee:

cat myfile.txt | tee "$(mktemp)" > /dev/null


cat myfile.txt | f=`mktemp` && cat > "${f}"