1

I'm pretty new to shell scripts and am only doing them because its about time I learnt and I need to for work.

I have been looking around and have tried multiple methods to get this working but can't seem to figure it out.

I have a script in which I want to access an SQLite database and store the result of a select statement in a variable.

What I've Tried So Far

This one just echoes whats inside the apostrophe. If I remove the dollar sign before the apostrophe I get the same outcome.

track_name=$'sqlite3 "$database_name" << EOF
select name from track where id = "$required_track";
exit;
EOF'

Here I get a syntax error near "track_name"

sqlite3 "$database_name" << EOF
track_name='select name from track where id = "$required_track";'
exit;
EOF

I have successfully executed the select statement without trying to store it in a variable but its not much use to me without being able to store it...

Any help would be much appreciated

1 Answer 1

2

To store the output of a command into a BASH variable you should use:

VAR_NAME=$(command);

For example, if you want to store your system current time into a variable or the results of a list directory command ejecution:

DATE_EXAMPLE_VAR=$(date); #Stores 'date' command output into DATE_EXAMPLE_VAR
echo $DATE_EXAMPLE_VAR; #Shows DATE_EXAMPLE_VAR contents

DIRCONTENTS=$(ls); #Stores a list of your current directory contents.

Similarly, this should work for sqlite3:

track_name=$(sqlite3 "$database_name" "select name from track where id = $required_track")
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this does not work for me. All its doing is echoing the statement. What is the end portion for | sqlite3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.