diff --git a/src/bin/psql/psqlscanslash.l b/src/bin/psql/psqlscanslash.l
index 86832a8..15ad610 100644
--- a/src/bin/psql/psqlscanslash.l
+++ b/src/bin/psql/psqlscanslash.l
@@ -19,7 +19,7 @@
 #include "postgres_fe.h"
 
 #include "psqlscanslash.h"
-
+#include "settings.h"
 #include "libpq-fe.h"
 }
 
@@ -53,7 +53,7 @@ static int	backtick_start_offset;
 #define LEXRES_OK			1	/* OK completion of backslash argument */
 
 
-static void evaluate_backtick(PsqlScanState state);
+static void evaluate_backtick(PsqlScanState state, bool to_hex);
 
 #define ECHO psqlscan_emit(cur_state, yytext, yyleng)
 
@@ -221,6 +221,13 @@ other			.
 					BEGIN(xslashbackquote);
 				}
 
+"``"				{
+					backtick_start_offset = output_buf->len;
+					*option_quote = '@';
+					unquoted_option_chars = 0;
+					BEGIN(xslashbackquote);
+				}
+
 {dquote}		{
 					ECHO;
 					*option_quote = '"';
@@ -354,10 +361,18 @@ other			.
 "`"				{
 					/* In NO_EVAL mode, don't evaluate the command */
 					if (option_type != OT_NO_EVAL)
-						evaluate_backtick(cur_state);
+						evaluate_backtick(cur_state, false);
 					BEGIN(xslasharg);
 				}
 
+"``"				{
+					/* In NO_EVAL mode, don't evaluate the command */
+					if (option_type != OT_NO_EVAL && *option_quote == '@')
+						evaluate_backtick(cur_state, true);
+					BEGIN(xslasharg);
+				}
+
+
 {other}|\n		{ ECHO; }
 
 }
@@ -693,7 +708,7 @@ dequote_downcase_identifier(char *str, bool downcase, int encoding)
  * as a shell command and then replaced by the command's output.
  */
 static void
-evaluate_backtick(PsqlScanState state)
+evaluate_backtick(PsqlScanState state, bool to_hex)
 {
 	PQExpBuffer output_buf = state->output_buf;
 	char	   *cmd = output_buf->data + backtick_start_offset;
@@ -750,7 +765,20 @@ evaluate_backtick(PsqlScanState state)
 		if (cmd_output.len > 0 &&
 			cmd_output.data[cmd_output.len - 1] == '\n')
 			cmd_output.len--;
-		appendBinaryPQExpBuffer(output_buf, cmd_output.data, cmd_output.len);
+
+		if (to_hex)
+		{
+			unsigned char  *escaped_value;
+			size_t			escaped_size;
+
+			escaped_value = PQescapeByteaConn(pset.db,
+								(const unsigned char *) cmd_output.data, cmd_output.len,
+														  &escaped_size);
+
+			appendBinaryPQExpBuffer(output_buf, (const char *) escaped_value, escaped_size);
+		}
+		else
+			appendBinaryPQExpBuffer(output_buf, cmd_output.data, cmd_output.len);
 	}
 
 	termPQExpBuffer(&cmd_output);
