From: pg on
Is there a way to make a Unix shell always store the output of its
command in a variable or a file, so that the output of the last
command is always available without re-running the command and without
copy-pasting from the terminal? Matlab has such a variable, which I
think is called "res".

There are various reasons for wanting to avoid re-running commands. It
might be difficult or impossible to get the same output by re-running
the command. It might take a long time or other resources. There are
also reasons for avoiding copy-pasting the result. It might require
reformatting, requires fiddling with the mouse, etc.

I guess it would be possible to encapsulate each command in a "tee"
command, but is it possible to make the shell do this automatically,
without the user having to modify his commands or think of it ahead of
time?
From: Bill Marcum on
On 2010-06-05, pg(a)gmail.com <phil.ganchev(a)gmail.com> wrote:
> Is there a way to make a Unix shell always store the output of its
> command in a variable or a file, so that the output of the last
> command is always available without re-running the command and without
> copy-pasting from the terminal? Matlab has such a variable, which I
> think is called "res".
>
The closest thing I can think of is the "script" command. Note that this
also captures the codes for cursor movement, color change, etc.

From: Tim Harig on
On 2010-06-05, pg(a)gmail.com <phil.ganchev(a)gmail.com> wrote:
> Is there a way to make a Unix shell always store the output of its
> command in a variable or a file, so that the output of the last
> command is always available without re-running the command and without
> copy-pasting from the terminal? Matlab has such a variable, which I
> think is called "res".

Yes, you can store the output; but, it is not automatic.

> There are various reasons for wanting to avoid re-running commands. It
> might be difficult or impossible to get the same output by re-running
> the command. It might take a long time or other resources. There are
> also reasons for avoiding copy-pasting the result. It might require
> reformatting, requires fiddling with the mouse, etc.

If you just want to run a variation of the previous command (or series of
commands); then, command history can help you quite a bit. For situations
where you absolutely do not want to run the command, in any form again,
you will need to manually save the output.

> I guess it would be possible to encapsulate each command in a "tee"
> command, but is it possible to make the shell do this automatically,
> without the user having to modify his commands or think of it ahead of
> time?

Somebody with more knowledge of shell internals may correct me; but, I
think the problem is that the shell does not normally access a programs
stdout stream unless explicitly asked to do. I suspect that unless
requested to do so, a shell simply does:
1. fork()s,
2. closeses all of its file descriptors except stdin, stdout, and
stderr,
3. and exec()'s the requested command with the child process
inhereting its own file descriptors
When redirects are requested I suspect the shell:
1. forks()s,
2. creates a pipe with pipe() ,
3. replaces the stdin, stdout, stderr with the proper file
descriptors (dup2()) from pipe() or its own stdin,
stdout, stderr file descripters as necessary for the
requested operation,
4. and exec()s the requested program.
Unless requested as the output program itself, ie, for an explicit variable
assignment, it doesn't read from the output pipe at all since the kernel
handles transfering data between the two file descriptors. If it did, it
would have to create 2 pairs of pipes, one from the first process, and
to a potential one to the second process and do all of the transfer itself.
This would create excessive overhead.

Therefore, I suspect that there is no way to implicitly copy the output
since the shell never internally accesses the information unless it
is redirecting to itself for something such as a variable assignment.
The closest you could do would be to create a function that saves the
output to a variable and then writes the variable to stdout. You would
have to call this function to exec the commands for any outputs that
you might want to use later.
From: Thomas 'PointedEars' Lahn on
pg(a)gmail.com wrote:

> Is there a way to make a Unix shell always store the output of its
> command in a variable or a file, so that the output of the last
> command is always available without re-running the command and without
> copy-pasting from the terminal? Matlab has such a variable, which I
> think is called "res".

While I think it might be possible to store all output using `exec' with a
sophisticated redirection and named pipes, ISTM you are simply looking for

res=$(command)

and the like. That would not satisfy the "always" criterion in your
question, but then again you probably do not *always* need (or want) this.


PointedEars
From: Keith Thompson on
"pg(a)gmail.com" <phil.ganchev(a)gmail.com> writes:
> Is there a way to make a Unix shell always store the output of its
> command in a variable or a file, so that the output of the last
> command is always available without re-running the command and without
> copy-pasting from the terminal? Matlab has such a variable, which I
> think is called "res".
>
> There are various reasons for wanting to avoid re-running commands. It
> might be difficult or impossible to get the same output by re-running
> the command. It might take a long time or other resources. There are
> also reasons for avoiding copy-pasting the result. It might require
> reformatting, requires fiddling with the mouse, etc.
>
> I guess it would be possible to encapsulate each command in a "tee"
> command, but is it possible to make the shell do this automatically,
> without the user having to modify his commands or think of it ahead of
> time?

I know of no way to do this unless you decided, before running a
given command, that you want to save its output. If you don't mind
doing that, I'm sure you can write a small alias or shell function
that does what you want.

As for capturing the output of *every* shell command, consider
that a command could produce arbitrarily long output. Also, should
the shell save the output (and, presumably separately, the stderr)
of just the most recent command, or of all commands?

Think about running a text editor in such a mode. The shell would
save everything, including terminal control sequences, which wouldn't
be terribly useful unless you want to replay the session.

It would be possible for a shell to provide a mode in which it does
this, but I'm not sure how practical it would be. I'd certainly
want it to be turned off by default.

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"