How to print and use a command output in a one-liner?

I’d like to pass the result of mktemp as argument to a command, let’s say gcc -o. gcc -o $(mktemp) causes the result to be used, but I need to figure out the result.

The only thing I could come up with is gcc -o $(out=$(mktemp); echo $out), but that doesn’t print the value to console, but instead it’s used as argument value which is correct afaik.

Is there any way to get the result of mktemp printed to console.

I’m capable to solve this in a script. I’d like to broaden my knowledge with the one-liner solution you hopefully propose.

I’d like to use this in bash on Ubuntu 19.04.

Asked By: Kalle Richter

||

How about tee with /dev/tty?

$ gcc -o $(mktemp | tee /dev/tty) hello.c
/tmp/tmp.UBSSnulNn2

$ /tmp/tmp.UBSSnulNn2
Hello, world!

Related:

Answered By: steeldriver

Switching things around in gcc -o $(out=$(mktemp); echo $out):

out=$(mktemp); echo "$out"; gcc -o "$out" hello.c

… which also conveniently leaves the path in the variable for later uses. (Presumably you don’t want to just see the path in the output to admire the beauty of mktemp‘s random name generation, right? You want that path to use it elsewhere.)

Answered By: muru
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.