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
  • 3
    This is not the return value, it's the output! – MirandaVeracruzDeLaHoyaCardina Aug 30 '18 at 13:14
  • 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". – Michael Dorst Jul 8 at 13:06

Run the command in the command substitution:

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

The output is now stored in the output variable which you can use as many times as you like.

  • 10
    This is not a correct answer. $? is. – mcmlxxxiii Aug 11 '16 at 21:50
  • @mcmlxxxiii: $? doesn't contain the paths. – choroba Aug 11 '16 at 21:54
  • 1
    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 '16 at 21:55
  • 3
    $? contains the exit status of the last command – Brett Wagner Nov 16 '17 at 17:18

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 $? – twnaing Mar 19 at 16:16
  • 1
    $? contains the exit code of the previous command, whatever that is. So, the answer is yes. – ahilsend Mar 21 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.

  • 2
    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 '16 at 16:38
  • 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 '16 at 0:51
  • One of my favourites, this is! Think I do this on a day-to-day basis. – MetalGodwin Oct 28 '16 at 11:20
  • 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), – Svend Hesselholt Henne Hansen Sep 14 '17 at 8:32

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.