I'm simply trying to get the output from a sql statement and store in bash variable. I am getting " unexpected EOF while looking for matching `)' " error. I don't see what i'm doing wrong. Why am I getting this error?

var=$($ORACLE_HOME/bin/sqlplus / as sysdba <<EOF  
select status from v\$instance;
  • This is odd: your command works for me. – G-Man Says 'Reinstate Monica' Oct 24 '16 at 21:23
  • Check "bash --version" That "<< EOF" part requires Bash version -ge 4.1. – Michael D. Oct 24 '16 at 21:28
  • @MichaelD., seems to work for me on version 3.1.17(1)-release, as well as on 4.3. – ilkkachu Oct 24 '16 at 21:35
  • @ilkkachu ok. my source was: tldp.org/LDP/abs/html/bashver4.html (Example 37-9. Using a here document to set a variable) – Michael D. Oct 24 '16 at 21:46

Is your script indented like that? the delimiter for the here-doc has to be at the beginning of the line. This works for me:

echo $(cat <<EOF
| improve this answer | |

Ikkachu is correct that the code, when not indented, should work fine on any reasonably modern version of Bash. However, I would add that you may find it easier to understand and debug if you use a "here string" instead of a "here doc":

var=$(sqlplus / as sysdba <<< 'select status from v$instance; exit;')

Or, use a standard pipeline:

var=$(echo 'select status from v$instance; exit;' | sqlplus / as sysdba)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.