I'd like to be able to use the result of the last executed command in a subsequent command. For example,
$ find . -name foo.txt
./home/user/some/directory/foo.txt
Now let's say I want to be able to open the file in an editor, or delete it, or do something else with it, e.g.
mv <some-variable-that-contains-the-result> /some/new/location
How can I do it? Maybe using some bash variable?
Update:
To clarify, I don't want to assign things manually. What I'm after is something like built-in bash variables, e.g.
ls /tmp
cd $_
$_
holds the last argument of the previous command. I want something similar, but with the output of the last command.
Final update:
Seth's answer has worked quite well. Couple of things to bear in mind:
- don't forget to
touch /tmp/x
when trying the solution for the very first time - the result will only be stored if last command's exit code was successful
Disclamers:
When running an interactive shell in tmux, you can easily access the data currently displayed on a terminal. Let's take a look at some interesting commands:
Yeah, this gives us a lot of possibilities now :) As for me, I set up a simple alias:
alias L="tmux capture-pane; tmux showb -b 0 | tail -n 3 | head -n 1"
and now every time I need to access the last line i simply use$(L)
to get it.This is independent of the output stream the program uses (be it stdin or stderr), the printing method (ncurses, etc.) and the program's exit code - the data just needs to be displayed.
There are more than one ways to do this. One way is to use
v=$(command)
which will assign the output of command tov
. For example:And you can use backquotes too.
From Bash Beginners Guide,
EDIT: After the edit in the question, it seems that this is not the thing that the OP is looking for. As far as I know, there is no special variable like
$_
for the output of last command.I just distilled this
bash
function from the suggestions here:Then, you just do:
Update: an anonymous user suggested to replace
echo
byprintf '%s\n'
which has the advantage that it doesn't process options like-e
in the grabbed text. So, if you expect or experience such peculiarities, consider this suggestion. Another option is to usecat <<<$grab
instead.As an alternative to the existing answers: Use
while
if your file names can contain blank spaces like this:As I wrote, the difference is only relevant if you have to expect blanks in the file names.
NB: the only built-in stuff is not about the output but about the status of the last command.
This is not strictly a bash solution but you can use piping with sed to get the last row of previous commands output.
First lets see what i have in folder "a"
Then, your example with ls and cd would turn to sed & piping into something like this:
So, the actual magic happens with sed, you pipe what ever output of what ever command into sed and sed prints the last row which you can use as parameter with back ticks. Or you can combine that to xargs also. ("man xargs" in shell is your friend)
You could set up the following alias in your bash profile:
Then, by typing 's' after an arbitrary command you can save the result to a shell variable 'it'.
So example usage would be: