10

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.

18

How about tee with /dev/tty?

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

$ /tmp/tmp.UBSSnulNn2
Hello, world!

Related:

8

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.)

  • Specifically, it might be a useful idea to rm -f "$out" afterwards. – ilkkachu Oct 7 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.