6

I have a problem with the MySQL output formatting while executing the commands from a bash script.

If I execute a command on the command line then, I am able to get the output in formatted as expected.

$ mysql -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"
+---------------------+---------------------+
| now()               | max(time_stamp)     |
+---------------------+---------------------+
| 2012-12-09 14:25:38 | 2012-12-09 14:25:20 |
+---------------------+---------------------+

But where as if I keep the same command in a script and execute I am not getting the formatted output.

$ cat test
#!/bin/bash
mysql -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"

$ ./test
now()   max(time_stamp)
2012-12-09 14:27:52     2012-12-09 14:27:47 

So all I need the same output from script.

Thanks.

2 Answers 2

13

Pass the -t or --table option to force table output.

mysql --table -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"

From mysql --help:

  -t, --table         Output in table format.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, It is working and giving the expected output.
0

If you want to have --table option enabled by default when calling mysql program, please check this SO answer How to store MySQL results to a file in table format.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.