ChameleonScales
ChameleonScales

Reputation: 165

Retain variable defined before "| yad --progress"

I'd like to define a variable within a process that is piped to a yad progress bar while retaining it outside of that process. e.g:

(a=3 ; sleep 1) | yad --progress --auto-close
echo $a

I'd also like to avoid writing that variable to a temporary file to retrieve later, especially considering it may sometimes be an array, associative array or whatever messy multiline string that would require more code to reconstruct from a file.

I think I should be able to use redirection rather than a pipe but I don't know how exactly in this situation and all my attemps have failed.

Upvotes: 2

Views: 85

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185219

Use this, using { } for grouping commands and >( ) for process substitution:

{ a=3; sleep 1; } > >(yad --progress --auto-close)
echo "$a"
  • you declare the variable in a subshell: ( ), the variable don't exists for the parent shell.

  • if you use a pipe: |, the command still be in a subshell, that's why the need for > >( ) process substitution.


Process Substitution >(command ...) or <(...) is replaced by a temporary filename. Writing or reading that file causes bytes to get piped to the command inside. Often used in combination with file redirection: cmd1 2> >(cmd2).

See http://mywiki.wooledge.org/ProcessSubstitution
http://mywiki.wooledge.org/BashFAQ/024

Upvotes: 4

Related Questions