Bash: dollar sign ($)

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

PID

$$: the process ID the current bash script is running under.
$?: the last command's exit code.

$!: the process ID of the last process started in the background.
e.g.:

./my_long_process &
echo $! // PID of my_long_process

Getting the process ID with sudo is not so straightforward, because the returned PID is that of the sudo process, not the child process.
Instead, you can save the PID of the child process to a file, for later re-use:

sudo -u apache sh -c "echo \$\$ > process.pid; exec ./script" &