Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active May 23, 2022 09:12
Show Gist options
  • Select an option

  • Save knbknb/e20a53aba0f7c9c13904740ad845d2b6 to your computer and use it in GitHub Desktop.

Select an option

Save knbknb/e20a53aba0f7c9c13904740ad845d2b6 to your computer and use it in GitHub Desktop.
Bash: multiline SQL statements for mysql with the heredoc syntax and vertical output
#!/usr/bin/env bash
mysql -u root -psecret -Dworld 2>/dev/null -E -t<<EOF
SELECT *
FROM city
WHERE NAME = 'amsterdam';
EOF
# /* expected result -- in vertical format */:
# *************************** 1. row ***************************
# ID: 5
# Name: Amsterdam1
# CountryCode: NLD
# District: Noord-Holland
# Population: 731200
## HOWEVER This will return nothing
# - if credentials are wrong, or
# - if these queries return an empty resultset
# */
################## Alternative ##############################
# /* password set in an extra config file:
# [client]
# user="root"
# password=secret
# host=localhost
# */
mysql --defaults-extra-file=$HOME/.mysql-credentials.cnf -Dworld -E -t<<EOF
SELECT *
FROM city
WHERE NAME = 'amsterdam1';
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment