0

how could i escaping ; or () or "" in echo ? I want to echo this command

"LOAD DATA INPATH '$i' INTO TABLE transient.url_log_201803 partition (ds='$TGLS', periode='$WKTS');"

but it says, syntax error near unexpected token}'` even i have changed that command with escaping single quote like this

'LOAD DATA INPATH '"'"$i"'"' INTO TABLE transient.url_log_201803 partition (ds='"'"$TGLS"'"', periode='"'"$WKTS"'"');'

I don't know how to echo that command into terminal or file. And also i tried to use \ for escaping and nothing

  • 1
    Your command doesn't have a } in it...are you showing us everything? Are you trying to prevent the variables from expanding? – Jesse_b Apr 6 '18 at 18:46
2

If you are trying to prevent the variables from expanding you need to escape the $ with \. It may seem unnecessary due to the variables being single quoted however the double quotes around the single quotes cause the single quotes to be interpreted literally:

$ echo "LOAD DATA INPATH '$i' INTO TABLE transient.url_log_201803 partition (ds='$TGLS', periode='$WKTS');"
LOAD DATA INPATH '' INTO TABLE transient.url_log_201803 partition (ds='', periode='');

Escape variables:

$ echo "LOAD DATA INPATH '\$i' INTO TABLE transient.url_log_201803 partition (ds='\$TGLS', periode='\$WKTS');"
LOAD DATA INPATH '$i' INTO TABLE transient.url_log_201803 partition (ds='$TGLS', periode='$WKTS');

If your intent was to keep the double quotes around the whole thing:

$ echo "\"LOAD DATA INPATH '\$i' INTO TABLE transient.url_log_201803 partition (ds='\$TGLS', periode='\$WKTS');\""
"LOAD DATA INPATH '$i' INTO TABLE transient.url_log_201803 partition (ds='$TGLS', periode='$WKTS');"
3

Don't try to use echo with complicated data.

The following uses a here-document:

cat <<END_SQL
LOAD DATA INPATH '$i' INTO TABLE transient.url_log_201803 PARTITION (ds='$TGLS', periode='$WKTS');
END_SQL

If you need to pipe this to a command such as mysql (this is a Hive SQL command, but I don't know what the Hive client is usually called, so I use mysql in the examples):

cat <<END_SQL | mysql
LOAD DATA INPATH '$i' INTO TABLE transient.url_log_201803 PARTITION (ds='$TGLS', periode='$WKTS');
END_SQL

If you need to pass it without expanding the variables:

cat <<'END_SQL' | mysql
LOAD DATA INPATH '$i' INTO TABLE transient.url_log_201803 PARTITION (ds='$TGLS', periode='$WKTS');
END_SQL

Using a here-document also allows you write more readable code (shorter lines):

cat <<'END_SQL' | mysql
LOAD DATA INPATH '$i'
INTO TABLE transient.url_log_201803
     PARTITION (ds='$TGLS', periode='$WKTS');
END_SQL

Note that the document contents and (more importantly) the end tag (END_SQL above) should not be indented:

for ...some loop range...; do
    cat <<END_SQL | mysql
LOAD DATA INPATH '$i'
INTO TABLE transient.url_log_201803
     PARTITION (ds='$TGLS', periode='$WKTS');
END_SQL
done

If the end tag is indented, the shell won't find it and you will get an error message about the here-document being terminated by end of file.

0

You do not need to escape parens or semicolons with echo:

$ echo "Semicolons look like ';'.  Parens look like '(' and ')'."
Semicolons look like ';'.  Parens look like '(' and ')'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.