Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to execute this and check if count is 0, 1 or > 1 in bash.

mysql -e "select count(*) from mydb.mydb;"

The output is:

| count(*) |
|        0 |

I don't know how to parse this. Also this will be used in a cron job, so I can't have any output.

share|improve this question
up vote 8 down vote accepted

You could suppress the tab column name by:

ROW_CNT=$(mysql --raw --batch -e 'select count(*) from mydb.mydb' -s)
echo $ROW_CNT

Also, the semicolon at end your SQL command is unnecessary

share|improve this answer
For Psql , I needed the -t option, eg ROW_CNT=$(psql -h AHOST -p APORT -d ADB -t -c 'select count( * ) from ATABLE ;') – AnneTheAgile Jul 24 '14 at 16:49

try out this-

count=$( $path/to/mysql -h $ip -u $user -p$password $schema -s \
          -e "select count(1) from employee where email=xyz@abc.com");

if [[ "${count}" = '0' ]]; then
    echo "Email exist, please enter a new value : "
share|improve this answer
Please explain what your code fragment does and how this answers the question! – Bananguin May 24 at 6:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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