Properly escape query for shell