Skip to main content

Fixed: "ERROR at line 1: Unknown command '\G'" in the mysql Client

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A statement ending in \G works when you type it at the mysql> prompt, and fails with ERROR at line 1: Unknown command '\G'. when it comes from --execute or a script. The command didn't go anywhere. The newer client just turns its own commands off when it isn't interactive.

What I Was Doing​

I wanted one row of a query printed vertically, from my terminal, without opening a session. I used the MySQL 9.7.4 client in Docker:

Terminal
docker compose exec mysql mysql --user=inventory_app --password --execute="SELECT Id, Name FROM categories LIMIT 1\G" inventory

What I Expected​

A vertical result, the same as at the prompt.

What I Got​

Output
Enter password:
ERROR at line 1: Unknown command '\G'.

The mysql client run with --execute failing with ERROR at line 1: Unknown command '\G'.

What I Checked​

The same statement at the prompt. Typed into an interactive mysql session, it works, as in How to Show MySQL Query Results Vertically with \G.

What the client says about its commands. \G is the short form of a client command, ego, listed on the mysql Client Commands page. The mysql Client Options page describes an option called --commands: "Whether to enable or disable processing of local mysql client commands." Its list of what gets disabled includes ego, and its default value is FALSE.

Why It Happens​

\G is never sent to the server. The client handles it, as one of its own local commands. In this version, those commands are off by default, and an interactive session is the case where they still work. So with --execute, the client finds a command it won't process, and reports it as unknown.

The Fix​

Use --vertical, which is the option for this. It prints every result vertically, with no \G:

Terminal
docker compose exec mysql mysql --user=inventory_app --password --vertical --execute="SELECT Id, Name FROM categories LIMIT 1;" inventory

Or turn the commands on, if the script you're running already has \G in it:

Terminal
docker compose exec mysql mysql --user=inventory_app --password --commands=ON --execute="SELECT Id, Name FROM categories LIMIT 1\G" inventory

After the Fix​

Output (either command)
Enter password:
*************************** 1. row ***************************
Id: 3
Name: Adapters

The mysql client with --vertical printing Id: 3 and Name: Adapters in vertical format.

Common Mistakes​

MistakeWhat happensDo this instead
Assuming a statement that works at the prompt works in a scriptLocal commands are treated differentlyTest the script form
Turning commands on for a script you don't fully trustIt can then run client commands such as systemPrefer --vertical
Removing \G and leaving no semicolonThe statement still runs with --execute, in a tableAdd --vertical for the vertical form

Troubleshooting​

SymptomLikely causeFix
The same error for \s, \u or sourceThey're client commands tooUse SQL, such as USE, or --commands=ON
A piped file fails on its first \GPiped input isn't interactiveAdd --commands=ON, or end statements with ; and use --vertical
Older scripts stopped working after an upgradeOlder clients processed these commandsUpdate the scripts

FAQ​

Is the server involved?​

No. The message comes from the client, before anything reaches the server.

Conclusion​

Outside an interactive session, the MySQL 9.7 client doesn't process its own commands, so \G is unknown to it. Use --vertical for vertical output, or --commands=ON for a script you trust.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.