Bash: How to invoke command and store the result in a variable?

后端 未结 2 936
既然无缘
既然无缘 2020-12-14 07:25

Basically I want to be able to invoke a given command, in this case mysql -uanon -ppwd -db mydb -e \"select count(*) from table1\". And then take this commands

相关标签:
2条回答
  • 2020-12-14 07:53
    $ A=$(mysql -uanon -ppwd -db mydb -e "select count(*) from table1")
    $ echo $A
    

    In other words, use the $() syntax.

    0 讨论(0)
  • 2020-12-14 08:01

    You most likely want to use batch mode (-B) and disable column names (--disable-column-names) for non-interactive mysql output:

    out=$(mysql -B -db mydb -uanon -ppwd --disable-column-names  -e "select count(*) from table1";)
    
    0 讨论(0)
提交回复
热议问题