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
I usually do what the others here have suggested ... without the assignment:
You can get fancier if you like:
I had a similar need, in which I wanted to use the output of last command into the next one. Much like a | (pipe). eg
to something like -
Solution : Created this custom pipe. Really simple, using xargs -
Basically nothing by creating a short hand for xargs, it works like charm, and is really handy. I just add the alias in .bash_profile file.
I think you might be able to hack out a solution that involves setting your shell to a script containing:
Then if you set
$PROMPT_COMMAND
to output a delimiter, you can write a helper function (maybe called_
) that gets you the last chunk of that log, so you can use it like:you can use !!:1. Example:
The shell doesn't have perl-like special symbols that store the echo result of the last command.
Learn to use the pipe symbol with awk.
In the example above you could do:
It's quite easy. Use back-quotes:
And then you can use that any time in the future