I have the following code in unix

 export PGPASSWORD=$3

 psql -h localhost -U postgres -d $DB_name -At -c "SELECT COUNT(*) AS USER_COUNT FROM pg_roles WHERE rolname = '${user}' " \
| while read -a Record ; 

echo 'count' $count
if   [[ $(($USER_COUNT)) -eq 0 ]]
echo 'New User ' $user
echo 'User Exists ' $user


It basically accepts a given username and checks if it exists in Postgresql. After we pass the arguments, it checks its count, which will be 1, if the username exists, otherwise 0, and stores it in the variable USER_COUNT. It then compares it with 0. But the comparison is fetching me wrong results. Regardless of whether a username exists or not, it only runs the 'True' part of if.

Edit-1 The reason I used if [[ $(($USER_COUNT)) -eq 0 ]] instead of if [$USER_COUNT -eq 0 ], as suggested by Phill, is because I was getting an error message otherwise, saying unary operator expected. I read somewhere that it is because $USER_COUNT is an unset variable.

Edit-2 Thanks for your suggestion Lennart. As suggested by him, I added -x to my first line and found that even though the value of $USER_COUNT is 1 when the user exists, and 0 otherwise, but while comparison, i.e in the step if [[ $(($USER_COUNT)) -eq 0 ]], $USER_COUNT takes the value 0, even if earlier it was 1. Hence, it always evaluates to true. But I am not sure what to do to resolve the above situation.

Edit-3 Thanks a lot Lennart. Your code solved my problem.

  • Should it not be if [[ $(($USER_COUNT)) -ne 0 ]] ? Commented Oct 29, 2019 at 11:11
  • @Lennart Thanks, I have edited my code. But anyways, the problem still persists. It never goes to the else part regardless of whether the user exists or not.
    – suvrat
    Commented Oct 29, 2019 at 11:14
  • Adding -x to shebang, i.e. #!/bin/bash -x may reveal what is going on Commented Oct 29, 2019 at 11:24

2 Answers 2


You may consider something like:

cnt=$(psql -h localhost -U postgres -d $DB_name -At -c "SELECT COUNT(*) AS USER_COUNT FROM pg_roles WHERE rolname = '${user}' ")

if [[ ${cnt} -eq 0 ]]
    echo 'New User ' $user
    echo 'User Exists ' $user

The error you got saying unary operator expected is indeed because the variable is unset. There are various ways to handle that, but in this case, I think it should be fine


if [[ $(($USER_COUNT)) -eq 0 ]]

Given that this is bash and you're only performing a simple, numeric comparison, shouldn't that "if" condition look more like this?

if [ $USER_COUNT -eq 0 ] 

Doubled square braces do something "more", allowing the use of regular expressions and other weirdness which you don't need here.

Personally, I wouldn't rely on a count to detect anything. Retrieve something definite from the record (like the name) and make sure you get something back - you could get a zero count back for any number of reasons.

Also, read up on using the Postgres Password File to get passwords into psql. https://www.postgresql.org/docs/9.1/libpq-pgpass.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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