Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k
Post Migrated Here from serverfault.com (revisions)
Source Link
denormalizer
  • 375
  • 2
  • 5
  • 14

Quoting / escaping / expansion issue in "command in a variable"

I want to run a command such as this in a bash script:

freebcp <authentication and other parameters> -t "," -r "\r\n"

When run directly on the command line, the command works as expected. But when placed in a variable in a bash script it returns an error such as this:

Msg 20104, Level 3
Unexpected EOF encountered in bcp datafile

Msg 20074, Level 11
Attempt to bulk copy an oversized row to the server

bcp copy in failed

When command is placed in a variable and double quotes are escaped:

cmd="freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t \",\" -r \"\r\n\" -c"
`$cmd`

Note: Putting the following in the script works as expected:

`freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t "," -r "\r\n" -c`

So I know there's some quoting/escaping/expansion problems but I can't figure out how to fix it.

Note 2: Single quoting -t -r parameters doesn't work either