zsh-users
 help / color / mirror / Atom feed
* capture stderr in command substitution
@ 2021-01-24 19:45 Ray Andrews
  2021-01-24 19:47 ` Axel Beckert
  2021-01-25 12:38 ` Stephane Chazelas
  0 siblings, 2 replies; 9+ messages in thread
From: Ray Andrews @ 2021-01-24 19:45 UTC (permalink / raw)
  To: Zsh Users

$ var=$(command)

... but $var doesn't pick up errors.  Can it be forced?



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-24 19:45 capture stderr in command substitution Ray Andrews
@ 2021-01-24 19:47 ` Axel Beckert
  2021-01-25 15:15   ` Ray Andrews
  2021-01-25 12:38 ` Stephane Chazelas
  1 sibling, 1 reply; 9+ messages in thread
From: Axel Beckert @ 2021-01-24 19:47 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

Hi,

On Sun, Jan 24, 2021 at 11:45:31AM -0800, Ray Andrews wrote:
> $ var=$(command)
> 
> ... but $var doesn't pick up errors.  Can it be forced?

I'd expect

  $ var=$(command 2>&1)

to do that.

		Kind regards, Axel
-- 
PGP: 2FF9CD59612616B5      /~\  Plain Text Ribbon Campaign, http://arc.pasp.de/
Mail: abe@deuxchevaux.org  \ /  Say No to HTML in E-Mail and Usenet
Mail+Jabber: abe@noone.org  X
https://axel.beckert.ch/   / \  I love long mails: https://email.is-not-s.ms/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-24 19:45 capture stderr in command substitution Ray Andrews
  2021-01-24 19:47 ` Axel Beckert
@ 2021-01-25 12:38 ` Stephane Chazelas
  2021-01-25 13:18   ` Stephane Chazelas
  1 sibling, 1 reply; 9+ messages in thread
From: Stephane Chazelas @ 2021-01-25 12:38 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

2021-01-24 11:45:31 -0800, Ray Andrews:
> $ var=$(command)
> 
> ... but $var doesn't pick up errors.  Can it be forced?
[...]

var=$(cmd>&2)

like in any Korn/POSIX-like shell.

To capture only stdout:

{ var=$(cmd 2>&1 >&3 3>&-); } 3>&1

That is we need to redirect cmd's stdout back to what it was
originally (after having redirected cmd's stderr to the pipe),
using that extra fd 3 temporarily.

That's also standard sh syntax.

-- 
Stephane


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-25 12:38 ` Stephane Chazelas
@ 2021-01-25 13:18   ` Stephane Chazelas
  0 siblings, 0 replies; 9+ messages in thread
From: Stephane Chazelas @ 2021-01-25 13:18 UTC (permalink / raw)
  To: Ray Andrews, Zsh Users

2021-01-25 12:38:00 +0000, Stephane Chazelas:
> 2021-01-24 11:45:31 -0800, Ray Andrews:
> > $ var=$(command)
> > 
> > ... but $var doesn't pick up errors.  Can it be forced?
> [...]
> 
> var=$(cmd>&2)
> 
> like in any Korn/POSIX-like shell.
[...]

D'oh sorry, that should be cmd 2>&1 (redirect fd 2 to the same
resource as fd 1 is redirected to (the writing end of the pipe
the other end of which is read by the shell to fillup $var)),
not cmd >&2 which would redirect stdout to the same resource as
fd 1 is redirected to, leaving none of cmd's file descriptor
connected to that pipe.

> To capture only stdout:
> 
> { var=$(cmd 2>&1 >&3 3>&-); } 3>&1
> 
> That is we need to redirect cmd's stdout back to what it was
> originally (after having redirected cmd's stderr to the pipe),
> using that extra fd 3 temporarily.
> 
> That's also standard sh syntax.
[...]

That one is correct.

-- 
Stephane


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-24 19:47 ` Axel Beckert
@ 2021-01-25 15:15   ` Ray Andrews
  2021-01-25 15:51     ` Vin Shelton
  0 siblings, 1 reply; 9+ messages in thread
From: Ray Andrews @ 2021-01-25 15:15 UTC (permalink / raw)
  To: zsh-users

On 2021-01-24 11:47 a.m., Axel Beckert wrote:
>
>    $ var=$(command 2>&1)
>
Thanks, superficial testing shows it working fine.  I finally sorta 
understand it too, you can read it as 'output #2 goes back to output #1.

var=$(cmd>&2)

That seems not to work but I'm in no mood to look into it so long as Axel's performs.

{ var=$(cmd 2>&1 >&3 3>&-); } 3>&1

... is not something that the eye's of a mortal should ever look upon ;-)

BTW (not worth starting a thread on this) how do you keep a command out of history?  I usta know this but can't remember and I've written it down somewhere but can't find it :(
I take a year off from coding anything and forget 101% of what I had learned.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-25 15:15   ` Ray Andrews
@ 2021-01-25 15:51     ` Vin Shelton
  2021-01-25 16:28       ` Pier Paolo Grassi
  2021-01-25 20:33       ` Ray Andrews
  0 siblings, 2 replies; 9+ messages in thread
From: Vin Shelton @ 2021-01-25 15:51 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 183 bytes --]

> how do you keep a command out of history?

You can

setopt HIST_IGNORE_SPACE

(capitalization and underscores don't matter) and put a space in front of
the command.

Regards,
  Vin

[-- Attachment #2: Type: text/html, Size: 1090 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-25 15:51     ` Vin Shelton
@ 2021-01-25 16:28       ` Pier Paolo Grassi
  2021-01-25 21:42         ` Bart Schaefer
  2021-01-25 20:33       ` Ray Andrews
  1 sibling, 1 reply; 9+ messages in thread
From: Pier Paolo Grassi @ 2021-01-25 16:28 UTC (permalink / raw)
  To: Vin Shelton; +Cc: Ray Andrews, Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 774 bytes --]

> how do you keep a command out of history?

I do it this way:

precmd(){
    [[ $________tmp___restore_history -eq 1 ]] && {
        unset ________tmp___restore_history
        fc -P
    }
}

execute_no_history(){
    # disable history, will be reactivated in precmd
    fc -p /dev/null
    ________tmp___restore_history=1

    zle accept-line
}
zle -N execute_no_history
bindkey '^[M' execute_no_history # my keybinding, customize it as you like

best

Pier Paolo Grassi


Il giorno lun 25 gen 2021 alle ore 16:55 Vin Shelton <
acs@alumni.princeton.edu> ha scritto:

> > how do you keep a command out of history?
>
> You can
>
> setopt HIST_IGNORE_SPACE
>
> (capitalization and underscores don't matter) and put a space in front of
> the command.
>
> Regards,
>   Vin
>
>

[-- Attachment #2: Type: text/html, Size: 2538 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-25 15:51     ` Vin Shelton
  2021-01-25 16:28       ` Pier Paolo Grassi
@ 2021-01-25 20:33       ` Ray Andrews
  1 sibling, 0 replies; 9+ messages in thread
From: Ray Andrews @ 2021-01-25 20:33 UTC (permalink / raw)
  To: zsh-users

On 2021-01-25 7:51 a.m., Vin Shelton wrote:
> setopt HIST_IGNORE_SPACE
>
> (capitalization and underscores don't matter) and put a space in front 
> of the command.
>
> Regards,
>   Vin
>
Thanks gentlemen.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: capture stderr in command substitution
  2021-01-25 16:28       ` Pier Paolo Grassi
@ 2021-01-25 21:42         ` Bart Schaefer
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2021-01-25 21:42 UTC (permalink / raw)
  To: Pier Paolo Grassi; +Cc: Vin Shelton, Ray Andrews, Zsh-Users List

On Mon, Jan 25, 2021 at 8:29 AM Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
>
> > how do you keep a command out of history?
>
> I do it this way:
> [...]
> zle -N execute_no_history
> bindkey '^[M' execute_no_history # my keybinding, customize it as you like

Simpler but same idea:

precmd() { add_history() { true } }
execute_no_history() {
  add_history() { false }
  zle accept-line
}
autoload -z add-zsh-hook
add-zsh-hook zshaddhistory add_history


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-01-25 21:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-24 19:45 capture stderr in command substitution Ray Andrews
2021-01-24 19:47 ` Axel Beckert
2021-01-25 15:15   ` Ray Andrews
2021-01-25 15:51     ` Vin Shelton
2021-01-25 16:28       ` Pier Paolo Grassi
2021-01-25 21:42         ` Bart Schaefer
2021-01-25 20:33       ` Ray Andrews
2021-01-25 12:38 ` Stephane Chazelas
2021-01-25 13:18   ` Stephane Chazelas

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git