Bash: Syntax error near unexpected token `)'

15,407

Use double quotes as much as you can.

config.conf should be like

host="localhost"
user="user"
password="GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4="
db_name="database"

And than your command:

mysqldump --lock-tables -h "${host}" -u "${user}" -p"${password}" "${db_name}" > dbbackup_`date +"%Y%m%d"`.bak

and you should be safe.

Share:
15,407

Related videos on Youtube

Mitchell Cash
Author by

Mitchell Cash

Updated on September 18, 2022

Comments

  • Mitchell Cash
    Mitchell Cash over 1 year

    I have created a shell script that uses information stored in a config file. The issue I am facing is when I try to pass certain values stored in the config file, for use by the shell script, I get a syntax error.

    For example:

    config.conf

    host=localhost
    user=user
    password=GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4= (not my real password)
    db_name=database
    

    script.sh

    # Load config file TODO: make this more secure
    source /path/to/config.conf
    
    # MySQL database dump
    mysqldump --lock-tables -h $host -u $user -p $password $db_name > dbbackup_`date +"%Y%m%d"`.bak
    

    This is what happens when I attempt to run the script:

    $ bash script.sh
    Syntax error near unexpected token `)'
    `password=GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4='
    

    The issue seems to be related to certain characters used in my randomly generated password. I have tried wrapping $password in quotes and when that didn't work I did the same with the actual password stored in the config file, but again I had no success.

    What's the best way to solve this problem?

    • Dababi
      Dababi over 7 years
      try using \ before the special characters in your password
  • Mitchell Cash
    Mitchell Cash over 7 years
    This still caused a slight issue: Got error: 1044: Access denied for user 'database'@'localhost' to database 'GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4=' when selecting the database. I seem to have now solved the problem by using your solution together with using the long form --password="$password"
  • stderr
    stderr over 7 years
    I edited the answer to be precise - there shouldn't be a space between -p and password string. Or to use --password as you do.