0

I am trying to make resquest to mysql database using bash script. I have found this example: bash script - select from database into variable

When I tried mysql mysql -u $user -p

user@user:~$ mysql mysql -u $user -p 
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 454
Server version: 5.7.18-0ubuntu0.17.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

This is find. But when I try mysql mysql -u $user -p $pwd

user@user:~$ mysql mysql -u $user -p $pwd
mysql  Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
                      (Defaults to on; use --skip-auto-rehash to disable.)
 .......................................

connect-expired-password          FALSE

I have checked $user and $pwd they contain correct username and password.

I must be doing something wrong, but I don't know what. How can I make a request to MySQL within a bash script? Why is this example not working as expected for me? Can you please help?

dmx
  • 1,862
  • 3
  • 26
  • 48

1 Answers1

3

From man mysql:

If you use the short option form (-p), you cannot have a space between the option and the password.

So it should be -p$pwd, or rather, "-p${pwd}".

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • Quote, absolutely. The braces are optional here, since there aren't any non-variable characters *after* `$pwd`. – chepner May 23 '17 at 17:19