I saw this line in bash script
kill -${2:-15} `cat $1.pid`
It's killing process but don't know what -${2:-15}
means.
Can anyone explain what it means?
I saw this line in bash script
kill -${2:-15} `cat $1.pid`
It's killing process but don't know what -${2:-15}
means.
Can anyone explain what it means?
It means expand the positional parameter $2
, or substitute with 15
if it is empty. So if the script is called with fewer than 2 arguments, the command will be:
kill -15 `cat $1.pid`
The <variable>:-<default>
syntax means "use $<variable>
if it's defined. Otherwise, use <default>
":
https://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion
In this case, $2
is a positional parameter, or the second word you pass to the script. This code makes it optional -- if it's not used or empty it will default to the string 15
.
The -15
flag sent to kill
tells it the type of signal to send ... in this case TERM
which should allow the process to quit gracefully.
For example, you could use:
script 123 9
This will send the KILL
signal to cat 123.pid
. I'm not sure what 123.pid
is, but it probably has the process ID of a known process you want to kill.
If you just did script 123
it would send TERM
instead.