If your shell is bash[1], you can try:
exec 3>&2 2>firefoxFile; firefox & exec 2>&3-
It's your shell (eg. bash) which prints that [jobnum] pid
background job notification to stderr, not firefox. This kludge temporarily redirects the stderr to the firefoxFile
file, capturing into it that notification and whatever firefox will write to stderr during its lifetime.
It will NOT capture the [jobnum] Done firefox
bash will print when the background job has terminated.
Your firefox & > file
will be parsed as two commands 1. firefox &
(which will run firefox
in the background) and 2. > file
(which will truncate file
without writing anything to it). That's most certainly not what you intended.
[1] you can read here why this trick doesn't work in other shells; while it's possible to redirect the stderr in zsh
, zsh
does not write its prompts and job notifications to stderr, but to another fd pointing to the current terminal, opened especially for this purpose.
&
runs in background, not forground. You seem to be using words foreground and background interchangably. – ctrl-alt-delor Feb 29 '20 at 19:25