How to escape a single quote inside a double quote string in bash script?












1















I am trying to run the follow bash code:



#!/bin/bash
export DEBIAN_FRONTEND=noninteractive

DB_USER='abc'
DB_USER_PASSWORD='123'
DB_MAGENTO='db_magento'

sudo apt-get update
echo debconf mysql-server/root_password password $DB_USER_PASSWORD | sudo debconf-set-selections
echo debconf mysql-server/root_password_again password $DB_USER_PASSWORD | sudo debconf-set-selections
sudo apt-get -qq install mysql-server > /dev/null # Install MySQL quietly
sudo mysql -uroot -p123 -e "CREATE SCHEMA ${DB_MAGENTO} DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


The error I am getting is ERROR at line 1: Unknown command '''.
Seems to be related with '${DB_USER}' .



How to escape these single quotes properly?



UPDATE



Afetr @Sergiy suggestion I am getting another error:



#!/bin/bash
export DEBIAN_FRONTEND=noninteractive

DB_USER='ila'
DB_USER_PASSWORD='123'
DB_MAGENTO='db_magento'

mysql -uroot -p123 -e "SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


Error:



$ sudo ./installMagento.sh 
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_magento |
| mysql |
| performance_schema |
| sys |
+--------------------+
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '123' at line 1









share|improve this question





























    1















    I am trying to run the follow bash code:



    #!/bin/bash
    export DEBIAN_FRONTEND=noninteractive

    DB_USER='abc'
    DB_USER_PASSWORD='123'
    DB_MAGENTO='db_magento'

    sudo apt-get update
    echo debconf mysql-server/root_password password $DB_USER_PASSWORD | sudo debconf-set-selections
    echo debconf mysql-server/root_password_again password $DB_USER_PASSWORD | sudo debconf-set-selections
    sudo apt-get -qq install mysql-server > /dev/null # Install MySQL quietly
    sudo mysql -uroot -p123 -e "CREATE SCHEMA ${DB_MAGENTO} DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


    The error I am getting is ERROR at line 1: Unknown command '''.
    Seems to be related with '${DB_USER}' .



    How to escape these single quotes properly?



    UPDATE



    Afetr @Sergiy suggestion I am getting another error:



    #!/bin/bash
    export DEBIAN_FRONTEND=noninteractive

    DB_USER='ila'
    DB_USER_PASSWORD='123'
    DB_MAGENTO='db_magento'

    mysql -uroot -p123 -e "SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


    Error:



    $ sudo ./installMagento.sh 
    mysql: [Warning] Using a password on the command line interface can be insecure.
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | db_magento |
    | mysql |
    | performance_schema |
    | sys |
    +--------------------+
    ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '123' at line 1









    share|improve this question



























      1












      1








      1








      I am trying to run the follow bash code:



      #!/bin/bash
      export DEBIAN_FRONTEND=noninteractive

      DB_USER='abc'
      DB_USER_PASSWORD='123'
      DB_MAGENTO='db_magento'

      sudo apt-get update
      echo debconf mysql-server/root_password password $DB_USER_PASSWORD | sudo debconf-set-selections
      echo debconf mysql-server/root_password_again password $DB_USER_PASSWORD | sudo debconf-set-selections
      sudo apt-get -qq install mysql-server > /dev/null # Install MySQL quietly
      sudo mysql -uroot -p123 -e "CREATE SCHEMA ${DB_MAGENTO} DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


      The error I am getting is ERROR at line 1: Unknown command '''.
      Seems to be related with '${DB_USER}' .



      How to escape these single quotes properly?



      UPDATE



      Afetr @Sergiy suggestion I am getting another error:



      #!/bin/bash
      export DEBIAN_FRONTEND=noninteractive

      DB_USER='ila'
      DB_USER_PASSWORD='123'
      DB_MAGENTO='db_magento'

      mysql -uroot -p123 -e "SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


      Error:



      $ sudo ./installMagento.sh 
      mysql: [Warning] Using a password on the command line interface can be insecure.
      +--------------------+
      | Database |
      +--------------------+
      | information_schema |
      | db_magento |
      | mysql |
      | performance_schema |
      | sys |
      +--------------------+
      ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '123' at line 1









      share|improve this question
















      I am trying to run the follow bash code:



      #!/bin/bash
      export DEBIAN_FRONTEND=noninteractive

      DB_USER='abc'
      DB_USER_PASSWORD='123'
      DB_MAGENTO='db_magento'

      sudo apt-get update
      echo debconf mysql-server/root_password password $DB_USER_PASSWORD | sudo debconf-set-selections
      echo debconf mysql-server/root_password_again password $DB_USER_PASSWORD | sudo debconf-set-selections
      sudo apt-get -qq install mysql-server > /dev/null # Install MySQL quietly
      sudo mysql -uroot -p123 -e "CREATE SCHEMA ${DB_MAGENTO} DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


      The error I am getting is ERROR at line 1: Unknown command '''.
      Seems to be related with '${DB_USER}' .



      How to escape these single quotes properly?



      UPDATE



      Afetr @Sergiy suggestion I am getting another error:



      #!/bin/bash
      export DEBIAN_FRONTEND=noninteractive

      DB_USER='ila'
      DB_USER_PASSWORD='123'
      DB_MAGENTO='db_magento'

      mysql -uroot -p123 -e "SHOW DATABASES;CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY ${DB_USER_PASSWORD};GRANT ALL PRIVILEGES ON * . * TO '${DB_USER}'@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;"


      Error:



      $ sudo ./installMagento.sh 
      mysql: [Warning] Using a password on the command line interface can be insecure.
      +--------------------+
      | Database |
      +--------------------+
      | information_schema |
      | db_magento |
      | mysql |
      | performance_schema |
      | sys |
      +--------------------+
      ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '123' at line 1






      bash mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 6 hours ago







      zwitterion

















      asked 7 hours ago









      zwitterionzwitterion

      3411516




      3411516






















          1 Answer
          1






          active

          oldest

          votes


















          2














          You should not need to use with double quotes:



          $ DB_USER='abc'; echo "test '${DB_USER}'"
          test 'abc'


          Alternatively, you can break the double quote, and escape the single quote - that's where it's necessary:



          $ DB_USER='abc';  echo "test"'"${DB_USER}"'
          test'abc'


          Yet another way would be to use printf with command substitution and using hex value for the quote (x27):



          sudo mysql -uroot -p123  -e "$(printf 'CREATE SCHEMA x27%sx27 DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER x27%sx27@x27localhostx27 IDENTIFIED BY %s ; GRANT ALL PRIVILEGES ON * . * TO x27%sx27@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;' ${DB_MAGENTO} ${DB_USER} ${DB_USER_PASSWORD}  ${DB_USER})"




          Side note: you have 3 commands in the script calling sudo. This is redundant. Just run the whole script as sudo myscript.sh and remove the need to add sudo to each of those lines.






          share|improve this answer


























          • hey @Sergiy, thanks for you suggestion. I've tried your first suggestion but I still getting an issue. I dont understand what could be. I put an update.

            – zwitterion
            6 hours ago








          • 1





            Ok I have found the issue. Should have a single quote in password also.

            – zwitterion
            6 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110668%2fhow-to-escape-a-single-quote-inside-a-double-quote-string-in-bash-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You should not need to use with double quotes:



          $ DB_USER='abc'; echo "test '${DB_USER}'"
          test 'abc'


          Alternatively, you can break the double quote, and escape the single quote - that's where it's necessary:



          $ DB_USER='abc';  echo "test"'"${DB_USER}"'
          test'abc'


          Yet another way would be to use printf with command substitution and using hex value for the quote (x27):



          sudo mysql -uroot -p123  -e "$(printf 'CREATE SCHEMA x27%sx27 DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER x27%sx27@x27localhostx27 IDENTIFIED BY %s ; GRANT ALL PRIVILEGES ON * . * TO x27%sx27@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;' ${DB_MAGENTO} ${DB_USER} ${DB_USER_PASSWORD}  ${DB_USER})"




          Side note: you have 3 commands in the script calling sudo. This is redundant. Just run the whole script as sudo myscript.sh and remove the need to add sudo to each of those lines.






          share|improve this answer


























          • hey @Sergiy, thanks for you suggestion. I've tried your first suggestion but I still getting an issue. I dont understand what could be. I put an update.

            – zwitterion
            6 hours ago








          • 1





            Ok I have found the issue. Should have a single quote in password also.

            – zwitterion
            6 hours ago
















          2














          You should not need to use with double quotes:



          $ DB_USER='abc'; echo "test '${DB_USER}'"
          test 'abc'


          Alternatively, you can break the double quote, and escape the single quote - that's where it's necessary:



          $ DB_USER='abc';  echo "test"'"${DB_USER}"'
          test'abc'


          Yet another way would be to use printf with command substitution and using hex value for the quote (x27):



          sudo mysql -uroot -p123  -e "$(printf 'CREATE SCHEMA x27%sx27 DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER x27%sx27@x27localhostx27 IDENTIFIED BY %s ; GRANT ALL PRIVILEGES ON * . * TO x27%sx27@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;' ${DB_MAGENTO} ${DB_USER} ${DB_USER_PASSWORD}  ${DB_USER})"




          Side note: you have 3 commands in the script calling sudo. This is redundant. Just run the whole script as sudo myscript.sh and remove the need to add sudo to each of those lines.






          share|improve this answer


























          • hey @Sergiy, thanks for you suggestion. I've tried your first suggestion but I still getting an issue. I dont understand what could be. I put an update.

            – zwitterion
            6 hours ago








          • 1





            Ok I have found the issue. Should have a single quote in password also.

            – zwitterion
            6 hours ago














          2












          2








          2







          You should not need to use with double quotes:



          $ DB_USER='abc'; echo "test '${DB_USER}'"
          test 'abc'


          Alternatively, you can break the double quote, and escape the single quote - that's where it's necessary:



          $ DB_USER='abc';  echo "test"'"${DB_USER}"'
          test'abc'


          Yet another way would be to use printf with command substitution and using hex value for the quote (x27):



          sudo mysql -uroot -p123  -e "$(printf 'CREATE SCHEMA x27%sx27 DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER x27%sx27@x27localhostx27 IDENTIFIED BY %s ; GRANT ALL PRIVILEGES ON * . * TO x27%sx27@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;' ${DB_MAGENTO} ${DB_USER} ${DB_USER_PASSWORD}  ${DB_USER})"




          Side note: you have 3 commands in the script calling sudo. This is redundant. Just run the whole script as sudo myscript.sh and remove the need to add sudo to each of those lines.






          share|improve this answer















          You should not need to use with double quotes:



          $ DB_USER='abc'; echo "test '${DB_USER}'"
          test 'abc'


          Alternatively, you can break the double quote, and escape the single quote - that's where it's necessary:



          $ DB_USER='abc';  echo "test"'"${DB_USER}"'
          test'abc'


          Yet another way would be to use printf with command substitution and using hex value for the quote (x27):



          sudo mysql -uroot -p123  -e "$(printf 'CREATE SCHEMA x27%sx27 DEFAULT CHARACTER SET utf8;SHOW DATABASES;CREATE USER x27%sx27@x27localhostx27 IDENTIFIED BY %s ; GRANT ALL PRIVILEGES ON * . * TO x27%sx27@'localhost'; FLUSH PRIVILEGES;SELECT Host,User,Password FROM mysql.user;' ${DB_MAGENTO} ${DB_USER} ${DB_USER_PASSWORD}  ${DB_USER})"




          Side note: you have 3 commands in the script calling sudo. This is redundant. Just run the whole script as sudo myscript.sh and remove the need to add sudo to each of those lines.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 6 hours ago

























          answered 6 hours ago









          Sergiy KolodyazhnyySergiy Kolodyazhnyy

          71k9147311




          71k9147311













          • hey @Sergiy, thanks for you suggestion. I've tried your first suggestion but I still getting an issue. I dont understand what could be. I put an update.

            – zwitterion
            6 hours ago








          • 1





            Ok I have found the issue. Should have a single quote in password also.

            – zwitterion
            6 hours ago



















          • hey @Sergiy, thanks for you suggestion. I've tried your first suggestion but I still getting an issue. I dont understand what could be. I put an update.

            – zwitterion
            6 hours ago








          • 1





            Ok I have found the issue. Should have a single quote in password also.

            – zwitterion
            6 hours ago

















          hey @Sergiy, thanks for you suggestion. I've tried your first suggestion but I still getting an issue. I dont understand what could be. I put an update.

          – zwitterion
          6 hours ago







          hey @Sergiy, thanks for you suggestion. I've tried your first suggestion but I still getting an issue. I dont understand what could be. I put an update.

          – zwitterion
          6 hours ago






          1




          1





          Ok I have found the issue. Should have a single quote in password also.

          – zwitterion
          6 hours ago





          Ok I have found the issue. Should have a single quote in password also.

          – zwitterion
          6 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110668%2fhow-to-escape-a-single-quote-inside-a-double-quote-string-in-bash-script%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          GameSpot

          connect to host localhost port 22: Connection refused

          Getting a Wifi WPA2 wifi connection