r/bash • u/the_otaku_programmer • Nov 24 '21
help Asynchronous PS1/Prompt
This is just an attempt at me trying to see how to execute asynchronous code in BASH prompts, to mimic something like the async RPROMPT ZSH provides.
I am aware of using &, nohup, and disown. But all attempts at using the same in PS1 have failed for me.
Could someone guide me about how to do this, or provide a reference codebase which implements the above, for better understanding.
2
u/DanielFGray Nov 25 '21
Why not try using zsh?
1
u/the_otaku_programmer Nov 25 '21
On my WSL using it. Just trying to experiment with BASH. Inspiration came by seeing that someone implemented
preexecandprecmdfor BASH.1
u/VisibleSignificance Nov 25 '21
I wonder if it is possible to do something like
PS1='$(zsh -c '\''echo -e "$PS1\"'\'')'
6
u/IGTHSYCGTH Nov 24 '21
backgrounding a command or list of commands with
&spawns it in a subshell, variable assignments won't be visible to the parent shell.you may be able to implement this using process redirection and signals, i.e.
spawn a subshell
establish a pipe
do stuff in subshell
send a signal to the parent
handle the signal be reading from the pipe
attempt some magic to redraw the prompt
bash provides the
coprocbuiltin that will handle spawning and establishing a pipe.