Bash variable change doesn't persist

前端 未结 2 529
[愿得一人]
[愿得一人] 2020-12-22 10:53

I have a short bash script to check to see if a Python program is running. The program writes out a PID file when it runs, so comparing this to the current list of running p

2条回答
  •  无人及你
    2020-12-22 11:23

    Commands after a pipe | are run in a subshell. Changes to variable values in a subshell do not propagate to the parent shell.

    Solution: change your loop to

    while read PID; do
        # ...
    done < $PIDFILE
    

提交回复
热议问题