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?

1
  • Please tag correctly. MySQL is not Oracle. The syntax is different. Commented May 23, 2017 at 16:30

1 Answer 1

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}".

Sign up to request clarification or add additional context in comments.

1 Comment

Quote, absolutely. The braces are optional here, since there aren't any non-variable characters after $pwd.

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.