bash - Trouble escaping quotes in a variable held string during a Sub-shell execution call -


i'm trying write database call within bash script , i'm having problems sub-shell stripping quotes away.

this bones of doing.

#---------------------------------------------     #! /bin/bash     export command='psql ${db_name} -f , -t --no-align -c "${sql}"  -o ${export_file} 2>&1'     psql_return=`${command}`     #--------------------------------------------- 

if use 'echo' print out ${command} variable output looks fine:

echo ${command} 

screen output:-

#---------------     psql drupal7 -f , -t --no-align -c "select distinct hostname accesslog;" -o /drupal/interfaces/exports/ip_list.dat 2>&1     #--------------- 

also if cut , paste screen output executes fine.

however, when try execute command variable within sub-shell call, gives error message. error psql client effect quotes have been removed around ${sql} string. error suggests psql trying interpret terms in sql string parameters.

so seems string , quotes composed correctly quotes around ${sql} variable/string being interpreted sub-shell during execution call main script.

i've tried escape them using various methods: \", \\", \\\", "", \"" '"', \'"\', ... ... can see 'try all' approach no expert , it's driving me mad.

any appreciated.

charlie101

instead of storing command in string var better use bash array here:

cmd=(psql ${db_name} -f , -t --no-align -c "${sql}" -o "${export_file}") psql_return=$( "${cmd[@]}" 2>&1 ) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -