113

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

$ find -name '*.wsdl'
./some/very/very/long/way/to/some/lost/directory/filename.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
Anto
  • 2,661

4 Answers4

101

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 {}
geirha
  • 46,101
ahilsend
  • 1,480
  • 2
  • 10
  • 10
  • Is it possible $? to return exit code of other command? e.g. ping 1.1.1.1 -w 10; return $? – Tun Mar 19 '19 at 16:16
  • 2
    $? contains the exit code of the previous command, whatever that is. So, the answer is yes. – ahilsend Mar 21 '19 at 20:42
64

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:

`!!`

e.g.

$ find . -name HardToFind.txt
some/crazy/path/to/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.

mit
  • 2,121
Rob
  • 751
  • 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 '16 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 '16 at 00:51
  • One of my favourites, this is! Think I do this on a day-to-day basis. – MetalGodwin Oct 28 '16 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), – Svend Hesselholt Henne Hansen Sep 14 '17 at 08:32
42

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"
choroba
  • 9,643
0

`!!` 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 -`'