$$ in a script vs $$ in a subshell

前端 未结 4 1804
既然无缘
既然无缘 2020-12-01 21:43

$$ gives process id of the script process when used in a script, like this:

Example 1

#!/bin/bash
# processid.sh
# print process ids

ps         


        
4条回答
  •  星月不相逢
    2020-12-01 22:09

    I tried and escaping (to pass the $$ to the subshell) does not work as the subshell inherits the $$ value from the parent bash. The solution to this is to use $BASHPID.

    (echo $$; echo $BASHPID)
    

    prints the PID from the parent shell and from the subshell.

提交回复
热议问题