mainThe GNU Bourne-Again SHell - Support: sr #110848, Feature Request: Make output of...

 
 

sr #110848: Feature Request: Make output of last command available via $ANS variable.

Submitter:  None
Submitted:  Mon 06 Mar 2023 02:06:26 AM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  1 - Wish Status:  Wont Do
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 17 Mar 2023 08:28:44 PM UTC, comment #5: 

That's quite a sense of entitlement you've got there, bro.

If you're trying to make a case for someone else to do work you're neither going to do or pay for yourself, this probably isn't the way to go about it.

Chet Ramey <chet>
Group administrator
Fri 17 Mar 2023 05:04:12 PM UTC, comment #4: 

Yeah, well, running it with a screen editor will turn any stupid interactive tool into a scriptable tool, like it should be.

E.g. vim will warn you, but

$ echo -e "iinput^[:x\n" | vim -T dumb file.txt > ans.out

will work just fine. (»^[« ← [Ctrl]-[V],[Esc])

But nevermind. I can see that you’re just a massive arrogant dick who is literally physically unable to think outside of the tiny box he enforces his sad realitoid to be in.
So maybe it’s time to replace the sad cruft of bash with a shell that’s not a complete mess of hacks worse than PHP but stuck in teleprinter times.

/squit

Anonymous
Thu 09 Mar 2023 09:51:59 PM UTC, comment #3: 

Why do you think this generalizes?

That construct is defined to run a command and capture its output through a pipe. Do you think that generalizes to any arbitrary command? How about commands that expect to be run interactively, such as screen editors?

Why do you think it would even work for something like, say, diff or cat? Are you proposing that the shell set up pipes to every foreground command to capture its output? Arbitrary amounts of output?

If you think this is of value -- I don't really see it -- write a shell function that takes a command as its argument, redirects its stdout to a temp file (or uses tee), and reads that output into a shell variable.

Then run it with a screen editor.

Chet Ramey <chet>
Group administrator
Thu 09 Mar 2023 08:30:04 PM UTC, comment #2: 

(In reply to comment #1)
Well, as I said, … what is e.g. `someVar="$( someCommand <( echo "$someOtherVar" ) | yetAnotherCommand )"`, if not I/O streams going through bash?

Anonymous
Mon 06 Mar 2023 02:13:54 PM UTC, comment #1: 

How do you think bash is involved in input/output to the commands it executes now? Do you think that input and output to executed commands goes through the bash process?


Chet Ramey <chet>
Group administrator
Mon 06 Mar 2023 02:06:26 AM UTC, original submission:  

Many calculators and other CLIs have the very useful feature, of being able to use the result of the last command inside the next one, even after you executed it. Without possibly expensive or harmful re-execution.

Over at Stackoverflow, we were discussing possible solutions for that, surprisingly missing feature, and apart from ugly hacks or having the actual terminal program log everything and scraping it out of there, the best solution would be, to patch bash to support the easiest solution by default:

Instead of directing stdin, stdout and stderr (&0, &1 and &2) from the executed commands straight to the terminal, one could have internal buffers (up to a certain limit, just like scrollback buffers), and give those to executed commands via the usual fifos or whatever bash uses internally right now, while `tee`ing what’s written to those buffers to the terminal aswell.

Then one could make those buffers available to scripts as $ANS (all three combined, like that bit of a terminal’s scrollback buffer) or $IN, $OUT and $ERR separately, or something like that.

Given that bash very likely already uses buffers for this internally, it shouldn’t be more work than having those variables as new builtins that point to those buffers, aswell as offering a way to configure the buffer sizes, plus either a pointer to mark where the last command’s output ended, or clear the buffers each time a prompt appears (like a `PROMPT_COMMANT+="clear"`).

See
https://stackoverflow.com/questions/24283097/reusing-output-from-last-command-in-bash/75629157#75629157
for my most advanced attempt at implementing this as a script in bash. Which hangs right now because it tries to avoid creating an internal buffer instead of just a named pipe.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-03-14 chet StatusNone Wont Do

    Back to the top

    Powered by Savane 3.13-4b48.
    Corresponding source code