$ mysql -e 'select a,b from tablefoo' databasename


| a | b |
| 1 | 0 |
| 2 | 1 |


$ mysql -e 'select a,b from tablefoo' databasename > file

yields a file file containing

a b
1 0
2 1

(where a tab is between the alphanumeric characters on each line).

I'd think that redirection shouldn't change the output. Why do I get two different results?

Edit: William Jackson's answer says that this is a feature of mysql: output format depends on whether the output is being redirected. This doesn't answer my question, though. How does mysql 'know' whether output is being redirected? Does redirection not just take the output and, well, redirect it somewhere? Shouldn't that be invisible to mysql?

  • Add the -t flag to your mysql command :)
    – spencer.sm
    Oct 1, 2018 at 4:28

1 Answer 1


Edit: I cannot be certain this is how mysql does it, but it could be using isatty(3) to determine whether STDOUT is a terminal or not, and modifying the output accordingly.

Edit 2: The mysql command line tool definitely uses isatty(). You can read the source code.

There are some good examples of this (although not in C) over at Stack Overflow:

To answer your question, "Why?": Because that is what the documentation says. See the reference manual:

When used interactively, query results are presented in an ASCII-table format. When used noninteractively (for example, as a filter), the result is presented in tab-separated format.

I suspect this decision was made for readability. When used interactively, mysql can assume a human is reading the output, and it is generally easier for us humans to read data that is delimited by those lines. When used non-interactively, the assumption is that another program is going to consume the output, and that tab-delimited output is easier to programmatically consume.

If you are interested in overriding this default and getting ASCII-table format when redirecting the output, you can use the --table (-t) command line option:

mysql -t -e 'select a,b from tablefoo' databasename > file
  • 1
    I am not interested in overriding the default. ¶ I didn't know whether the difference in output was due to something in mysql or was due to something in bash. But this doesn't fully answer my question. How does mysql know what's happening to its output? I'd think the redirection should be invisible to mysql. I've edited the question to state more clearly what I mean to ask. +1, though, for answering very nicely the question I (apparently) seemed to be asking.
    – msh210
    Oct 31, 2011 at 18:37
  • Ah, I misunderstood. I updated my answer. Oct 31, 2011 at 19:12
  • Ah, that helps. But it's still not really what I want, which is "how does isatty 'know'?" That is, isatty is called by mysql. Whether stdout is a tty is a fact of life in bash, not in mysql. How does mysql know what's going on in bash? I would think stuff in bash would be invisible to mysql.
    – msh210
    Oct 31, 2011 at 20:23
  • 1
    Stdout is a file descriptor, and bash can use isatty() the same as any other program to determine if it is pointing to a tty or a file on disk. isatty() is provided by the OS, not bash. Oct 31, 2011 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .