bashshellyad

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.


Solution

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

    { a=3; sleep 1; } > >(yad --progress --auto-close)
    echo "$a"
    

    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