1

Suppose I would like to see, how the following expression will work in if statement

[ `ps -p 4800 -o comm=` == "ssh-agent" ]

But I don't want to write special script for this. Can I compute and/or execute this statement just in live shell to see which boolean value it returns?

If I put this into command line, it swears.

$ [ `ps -p 4800 -o comm=` = "ssh-agent" ]
-bash: [: =: unary operator expected

$ [ `ps -p 4800 -o comm=` == "ssh-agent" ]
-bash: [: ==: unary operator expected
2
  • What does it output?
    – choroba
    Feb 5 at 18:17
  • 1
    [ .... ] && echo match || echo not_match? Feb 5 at 18:17
3

If the ps returns nothing, the first string disappears and [ only sees two arguments. Use double quotes to fix it.

[ "`ps -p 4800 -o comm=`" = ssh-agent ]
echo $?

$? will be 0 if the condition returns true.

BTW, you don't need the double quotes around ssh-agent as none of its characters is special.

2
  • But putting double quotes around both sides of a string compare may be a good habit to get into
    – spuck
    Feb 5 at 21:10
  • 1
    @spuck: Understanding when single or double quotes are needed is even better :-)
    – choroba
    Feb 6 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.