0

Heyo all,

Say I have a bash file.

echo "Hey!"
echo "Hay!"
bash ~/otherstuff.sh &

It's the last line that annoys me. I'd like it to be done in the background, which it technically is. But every time it starts, it outputs [1] 21345, i.e. the process ID. I've tried every reroute I know of, none work.

TL;DR hide Procees output, and is it even considered an stdout?

The best I have is sh -c '. ~/otherstuff.sh' & disown, which hides the "completed" output, but not the original output.

Cheers! Bobbbay

1

This [1] 21345 is not from the process you run asynchronously, it's from the interactive Bash you're working in.

Non-interactive bash or sh should not write this, so let it run the task asynchronously:

bash -c 'whatever &'
# or
sh -c 'whatever &'

Notes:

  • This will redirect stdin of whatever to /dev/null or equivalent file.
  • The task will not become a job in the main shell (jobs will not list it), you won't be able to simply bring it to the foreground with fg.

To run whatever & as a job in the main shell without printing [1] 21345 or so, you need to juggle file descriptors of the main shell. Bash prints [1] 21345 to its stderr, we need to redirect it temporarily:

exec 12>&2 2>/dev/null; whatever & 2>&12; exec 2>&12 12>&-

Notes:

  • This uses file descriptor 12. Choose another number if it collides with anything you're doing (or use {varname}>…).
  • whatever will run with stderr restored.
  • Any problem the main shell encounters while actually running whatever (e.g. command not found) will be silenced, because at the time the stderr of the shell is /dev/null.
  • Still you will see [1] Done or so after the job finishes.
3
  • Hey, thanks for the reply! Interestingly this is crashing my alacritty windows every time I run it. Ideas?
    – Bobbbay
    Sep 7 '20 at 21:22
  • @Bobbay There are two distinct commands in my answer. Which one do you mean by "this"? Does "this" misbehave with any command? or only with otherstuff.sh? What does the script do? Do other terminal emulators crash? Sep 7 '20 at 22:02
  • The second command you wrote, using zsh -c ". ~/.initrc". Same thing goes for any command in there, e.x. echo "Hello!", this happens with xterm as well (I can try with your terminal emulator, if needed ;) )
    – Bobbbay
    Sep 8 '20 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.