Simple scenario: I'm looking for a wsdl file lost in the middle of a project.

$ find -name '*.wsdl'

Now that I know where it is, I want to do something with this file, say edit it. Instead of copy/pasting the path behind my command, is it possible to use the path returned by find earlier ? Just like it's possible to access the last argument you've typed with !$ or you last command with !!.
I've read that it was possible with $?, but it only returns me an error: 0: command not found

$ echo $?
0: command not found
  • 7
    This is not the return value, it's the output! Aug 30, 2018 at 13:14
  • 2
    To elaborate, the "return value" is always a number. Usually 0 means success, and non-zero means some kind of failure. The string (or text) that the command spits out is referred to as its "output", not its "return value". Jul 8, 2019 at 13:06

4 Answers 4


There is no special bash variable for that.

$? contains the exit code of the last command (0 = success, >0 = error-code)

You can use the output of find with the -exec flag, like this:

 find -name  '*.wsdl' -exec emacs {} \;

The {} is replaced with the file name found by find. This would execute the command for every found file. If you want to execute a command with all found files as arguments use a + at teh end like this:

  find -name '*.wsdl' -exec emacs {} +

This would open one emacs instance with all found .wsdl files opened in it.

A more general solution is to store the output in a variable:

result=$(find -name '*.wsdl')
emacs $result

This works with all commands, not just find. Though you might also use xargs:

  find -name '*.wsdl' | xargs emacs {}
  • Is it possible $? to return exit code of other command? e.g. ping -w 10; return $?
    – Tun
    Mar 19, 2019 at 16:16
  • 2
    $? contains the exit code of the previous command, whatever that is. So, the answer is yes.
    – ahilsend
    Mar 21, 2019 at 20:42

Here's a quick hack that should do what you want with minimal keystrokes, if you don't mind that the last command is executed twice.

Use backtick, ala:



$ find . -name HardToFind.txt
$ vim `!!`

*edit: I see the above linked "possibly duped" question also contains this answer. still relevant directly to this one, so leaving it, but sorry for the dupe.

  • 4
    Very clever. I think this should be the accepted answer as it solves the most common case of not knowing you need the output until you've already run the command.
    – Paul Ruane
    Apr 19, 2016 at 16:38
  • 1
    I agree, this is what I came here looking for. Doing anything on the previous line defeats my purpose, to retroactively decide I want to do something with the last returned value. (Checkout a branch I found via git br | grep in my case)
    – Jack Casey
    Jul 21, 2016 at 0:51
  • One of my favourites, this is! Think I do this on a day-to-day basis. Oct 28, 2016 at 11:20
  • 1
    I think this is very clever that you can do that, but not sure how useful it really is. Is typing !! is that much easier than pressing the up key? And then you can still see your command. If running the command again is acceptable, I think just pressing up is just as easy (and a little more understandable and transparent), Sep 14, 2017 at 8:32

Run the command in the command substitution:

output=$(find -name '*.wsdl')

The output is now stored in the output variable which you can use as many times as you like, with the following command:

echo "$output"
  • 18
    This is not a correct answer. $? is.
    – mcmlxxxiii
    Aug 11, 2016 at 21:50
  • 5
    @mcmlxxxiii: $? doesn't contain the paths.
    – choroba
    Aug 11, 2016 at 21:54
  • 4
    My apologies, I was confused by the misuse of the term return in the question. Your answer is absolutely correct for the above question.
    – mcmlxxxiii
    Aug 12, 2016 at 21:55
  • 9
    $? contains the exit status of the last command Nov 16, 2017 at 17:18
  • This was the answer that I was looking for. I'm interested in the output of the previous command not the success/fail return value. So thank you! Oct 17, 2019 at 1:59

`!!` is a great solution, but if you want to be even quicker you could use aliases.

Unfortunately it won't work:

~$ alias test='echo `!!`'
~$ test
zsh: command not found: !!

So instead use `fc -e -` Example aliases I use:

copy output:

alias co='echo `fc -e -` | xclip -in -selection clipboard'

open with vim; if output has many lines, opens all of them in tabs:

alias vo='vim -p `fc -e -`'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .