1

Let's imagine I want to save the text of various bash script in my database with sqlite3.

If my script do this

VARIABLE=$(cat "bashScript.sh")
sqlite3 mydb.db "CREATE TABLE data (myBash BLOB)" 
sqlite3 mydb.db "INSERT INTO data VALUES (${VARIABLE})"

Because the bash script will have all the special character, this will not work. How can I do this?

1 Answer 1

2

You need to quote the value in the Insert statement, and you'll want to protect quotes in the value itself: untested:

sql_value=$(sed 's/'\''/&&/g' <<< "$VARIABLE")
sqlite3 mydb.db "insert into data values ('$sql_value')"
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea, use '' inside a "", so the $sql_value work. The unique character I have to worry is the '? What you do is replace all ' by &&? And what the <<< really do? I search and don't find the mean of 3 <
In the right-hand side of sed's s/// command, a & means "use whatever was matched on the left-hand side" -- so I'm replacing each single quote with 2 single quotes. The <<< is a bash here-string

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.