How to return to bash prompt after printing output from backgrounded function? How to return to bash prompt after printing output from backgrounded function? bash bash

How to return to bash prompt after printing output from backgrounded function?


What is the problem you're trying to solve?

Right now, this is more or less a cosmetic problem. You're still in the shell, and the prompt is still there. Just type another command and it will be executed.

Alternatively, run the function in the foreground, or if you need to do something else in between, use wait:

$ fn & pid=$!$ : something else$ wait ${pid}


Compile below code to file a.out

#include <sys/ioctl.h>#include <sys/stat.h>#include <fcntl.h>int main(int argc, char *argv[]){    /* char buf[] = "date\n"; */    char buf[] = "\n";  /* Data to write on terminal */    int i;    int fd = open(argv[1], O_WRONLY);  /* Open terminal */    /* printf("fd = %d\n", fd); */    for (i = 0; i < sizeof buf - 1; i++)  /* Write data */      ioctl(fd, TIOCSTI, &buf[i]);    close(fd);  /* Close file descriptor */    return 0;}

This program expects a path as command line argument.Program will open the path and write a new line to this path.

If this path happen to contain the file descriptor of a writable terminal running bash script, this would cause bash to catch a new prompt.

Modify your shell script

fn(){        sleep 10        echo "Done"        ./a.out /proc/$PPID/fd/0}fn &

This script would do some job (represented with sleep here) and then call the utility written previously with argument as input terminal of parent. Parent terminal would receive a new line and catch a new prompt discarding the stray command on this prompt if any.

/proc contains directories for all processes. Name of folder matches to the pid of process.Inbuild variable PPID contains the parent's pid.Inside the pid directory, there is an fd directory containing open streams.0 is for input, 1 is for output and 2 is for error. There may be more open streams depending on the process. We are interested in 0 stream here.


similar to Henk Langevelds solution.

Find the pid of your script
wait for it to finish.
echo a line
Prompt is back
Unfortunately you're going to get that blank line still

#!/bin/bashfn(){    sleep 1    echo "Done"}fn &PID=$!wait $PIDecho -e ''