Results 1 to 7 of 7

Thread: Command substitution problem with bash, sqlite3

  1. #1
    Join Date
    Apr 2009
    Beans
    300
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Command substitution problem with bash, sqlite3

    Scipt:
    Code:
    #!/bin/bash
    
    cmd='sqlite3 test.db "select col1, col2 from table1;"'
    
    result=$($cmd) # doesn't work
    
    #result=$(sqlite3 test.db "select col1, col2 from table1;") # this works when uncommented
    
    echo $result
    When executed gives the following error:
    Code:
    sqlite3: Error: too many options: "col1,"
    Use -help for a list of options.
    However if I uncomment line 4 it works fine.

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Command substitution problem with bash, sqlite3

    See this post and this answer. What you get after $cmd gets expanded to its value is equivalent of
    Code:
    result=$(sqlite3 test.db \"select col1, col2 from table1;\")
    In bash, you should use an array for this to work as intended:
    Code:
    #!/bin/bash
    
    cmd=(sqlite3 test.db 'select col1, col2 from table1;')
    result=$("${cmd[@]}")
    or use zsh instead of bash.
    Last edited by schragge; January 16th, 2015 at 11:06 PM.

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Command substitution problem with bash, sqlite3

    Why do you want to store the command in a variable? If you have to invoke it multiple times, then use a function.
    Last edited by sisco311; January 17th, 2015 at 01:06 AM. Reason: typo

  4. #4
    Join Date
    Apr 2009
    Beans
    300
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: Command substitution problem with bash, sqlite3

    Quote Originally Posted by schragge View Post
    See this post and this answer. What you get after $cmd gets expanded to its value is equivalent of
    Code:
    result=$(sqlite3 test.db \"select col1, col2 from table1;\")
    In bash, you should use an array for this to work as intended:
    Code:
    #!/bin/bash
    
    cmd=(sqlite3 test.db 'select col1, col2 from table1;')
    result=$("${cmd[@]}")
    or use zsh instead of bash.
    @schragge: Thank you. It works.

    @sisco311: That script was for illustrative purposes only, line 4 was commented.

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Command substitution problem with bash, sqlite3

    Quote Originally Posted by donsy View Post
    @schragge: Thank you. It works.

    @sisco311: That script was for illustrative purposes only, line 4 was commented.
    I know. That's why I asked why do you want to store the sqlite3 command and its parameters in a variable. This seems like a strange problem to want to solve.

  6. #6
    Join Date
    Apr 2009
    Beans
    300
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: Command substitution problem with bash, sqlite3

    Quote Originally Posted by sisco311 View Post
    I know. That's why I asked why do you want to store the sqlite3 command and its parameters in a variable. This seems like a strange problem to want to solve.
    I'm always open to suggestion. How would you do it?

  7. #7
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Command substitution problem with bash, sqlite3

    Quote Originally Posted by donsy View Post
    I'm always open to suggestion. How would you do it?
    Do what?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •