Inside a bash script, how to get PID from a program executed when using the eval command? Inside a bash script, how to get PID from a program executed when using the eval command? unix unix

Inside a bash script, how to get PID from a program executed when using the eval command?


Your ampersand is backgrounding the eval line, causing the (top-level) shell to fork a child, the child shell to eval the string and in turn run your java program as a grandchild of the top-level shell. So, $! reports the pid of the child shell, which is the most recently backgrounded command.

Instead move the backgrounding inside your eval:

eval "(java ...) &"pid=$!

As long as the parenthetical doesn't get complicated enough to become a subshell, the above will work.