tags:

views:

92

answers:

8

In a linux shell, is it possible to return the output of the last command. I realize I could have piped it or sent the output to a file, but my requirement is to retrieve that output after the command has been run.

Using csh, but would hear about any shell.

Edit For some context: Not until midway through a 3 hour build script, I'll remember that I want to see something at the beginning of the output after it's done. At this point I've exceeded the number of lines in my terminal so I can't scroll up to see it (or the beginning is hard to find). Of course I can be better about storing my output, but I've always been curious if this were possible.

+4  A: 

No. The output of a program never passes through the shell's hands. Without redirection, it goes straight to the TTY. With redirection, it goes straight to whatever file or pipe it was directed to. The shell has no idea what the process sent to stdout/stderr.

Nicholas Knight
Thanks, that's what I wanted to know
AK
A: 

You can see the return code of the last command, at least in bash, with $?.

As far stdout goes, I don't think you can.

sheepsimulator
+1  A: 

save output as a variable?

bash: Output=$(ls); echo $Output
aaa
A: 

How about using substitution. Like this:

command1
command2
RESULT=`LASTCOMMAND`
echo $RESULT

NawaMan
A: 

My be you can use tee command to put the output to STDOUT as well to a file and then use it.

Raghuram
+1  A: 

This could get very messy with certain commands that use cursor addressing. What's the output of your vim session? Or a slightly more sane example: You're downloading a file with wget, and it shows a percentage bar going from 0% to 100%. What's the output of that?

I think you basically want a hardcopy of anything between your current line and the previous prompt. That's not something shell redirection would be a good example for, that's basically something a terminal would do. You could do it manually by copying and pasting in screen, and maybe you could automate it with its exec command. tmux might have something similar, and you can do weird things with the shell mode(s) of Emacs, but that's a rather heavyweight solution. rxvt-unicode has a perl extension

mhd
+5  A: 

script(1) is exactly what you need:

script
make
exit
vim typescript

The script program will start a new shell and save input and output to the typescript file. When you're done, just close the shell with exit or ^D. If you'd rather not start a new shell but just run your build, you can use: script -c <command>.

sarnold
That's cool. I like how you can use it to log a whole batch of commands.
AK
A: 

This is a function of your terminal. You should focus on creating a setup where you don't have to remember to do anything. One possible solution is to launch "screen" in your login script (which is not so easy to get right) with logging enabled.

embobo