1👍
✅
Usually such error messages are output on the stderr
stream, while the $()
construct returns only the stdout
stream. You need to redirect stderr
to stdout
:
k=$(kill -HUP 1234 2>&1)
Your regular expression has an error: the *.
at the end must be .*
; you will also need to quote your strings in your test:
if [[ "$k" =~ .*No\ such\ process.* ]]
Note that "$k"
is quoted, while the spaces in the regular expression are escaped; you cannot quote the regular expression (as ".*No such process.*"
), as using a quoted string in a =~
test forces string match instead of a regular expression match.
Source:stackexchange.com