Search code examples
unixposixnohup

Unexpected nohup behaviour


I have an executable file test, it contains

a="$RANDOM"
echo "$a">>out

Now, if I simply ./test then out contains a random number. But if I nohup ./test & then out is empty. Why?


Solution

  • Transferring comment into an answer

    Are you on Ubuntu (or another Linux distro)? Is /bin/sh a link to /bin/dash rather than /bin/bash? If so, when you run it with bash as your shell, then $RANDOM works, but when nohup runs it with /bin/sh (aka dash), you get nothing.

    You might be able to fix it by using #!/bin/bash as the shebang line.

    Actually I'm on Debian. But anyway I forgot the shebang indeed; now it works perfectly.

    Great!

    Incidentally, however tempting it is, it's generally a bad idea to call programs test because there is a shell built-in called test (also known as [) that can readily cause confusion if it is run instead of your test program.