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.

Kalle Richter
  • 2,100
  • 4
  • 20
  • 37

2 Answers2

18

How about tee with /dev/tty?

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

$ /tmp/tmp.UBSSnulNn2
Hello, world!

Related:

steeldriver
  • 78,509
  • 12
  • 109
  • 152
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.)

muru
  • 69,900
  • 13
  • 192
  • 292