Jimm
Jimm

Reputation: 8505

stop bash from expanding * in sql statement

i have a delete statement delete * from table_name;. Currently shell expands it to list all existing files in current directory. How can i escape it, so that the string that is passed to sqlplus is indeed "delete * from table_name". I tried \* '*' and \\* and none of them work.

The exact script is

#!/bin/bash
table_name = $1
delete_sql= "delete * from $table_name;"
echo $delete_sql > abc.sql

Upvotes: 2

Views: 177

Answers (2)

schesis
schesis

Reputation: 59118

You just need to quote the variable (and fix your spacing around the = sign):

#!/bin/bash
table_name=$1
delete_sql="delete * from $table_name;"
echo "$delete_sql" > abc.sql

Upvotes: 3

Nate from Kalamazoo
Nate from Kalamazoo

Reputation: 436

How about

echo "delete * from table_name" | sqlplus 

or

echo "delete * from table_name" > mystatements.txt
sqlplus @mystatements.txt

On a side note, you don't need to specify * in a delete statement - all fields in the matching rows are deleted.

Upvotes: 5

Related Questions