How to get variable from text file into Bash variable

AndyW picture AndyW · Dec 30, 2011 · Viewed 15.1k times · Source

Simple question, in BASH I'm trying to read in a .pid file to kill a process. How do I read that file into a variable. All the examples I have found are trying to read in many lines. I only want to read the one file that just contains the PID

#!/bin/sh
PIDFile="/var/run/app_to_kill.pid"
CurPID=(<$PIDFile)

kill -9 $CurPID

Answer

SiegeX picture SiegeX · Dec 30, 2011

You're almost there:

CurPID=$(<"$PIDFile")

In the example you gave, you don't even need the temp variable. Just do:

kill -9 $(<"$PIDFile")