I just need to pass any HTML content as a command line argument which includes characters like ', " , ` and what not? How can I pass this as a single argument in unix?
4 Answers
If you were typing at the command line, you could use the following hack:
some_program -some_option some_argument "$(cat)"
any text you like with ` or " or $ or < or whatever
^D
The last thing is control-D (end of file)
To put that into a bash script, you can use a Here document:
some_program -some_option some_argument "$(cat <<'EOF'
any text you like with ` or " or $ or < or whatever
EOF
)"
That will work as long as the text is not exactly the characters EOF (and if it is, you just have to change EOF in both places to something else).
But I don't think either of those are what you want to do. I think you are trying to invoke a program from another program (in Lua). Unfortunately, Lua out-of-the-box does not provide any function which can do that. It only provides os.execute, which uses the shell (/bin/sh, not bash) to interpret the command line.
There was a nice implementation for Lua of spawn
written by Mark Edgar, but I don't know if it is still maintained. Failing that, you can still use the second hack:
require("os")
function call_with_arg(prog, rawtext)
return os.execute(prog.." \"$(cat <<'EOF'\n"..rawtext.."\nEOF\n)\"")
end
local html =
[=[<html><body><img src="image.png" onload='alert("loaded")'/></body></html>]=]
call_with_arg("echo >somefile.txt", html)

- 234,347
- 28
- 237
- 341
-
And how to join `Here-doc`'s result back, to get a single line? – Top-Master Apr 25 '23 at 04:38
I was looking for an answer to the question but I don't really see it here, so here is the solution I ended up with (not I'm not passing HTML or things like that, but similar)
for example the psql
tools takes commands that can be multi lines, but they need to be passed in a double quoted string.
I started with other answers that recommended the following:
CMD=$(cat << EOM
CREATE TABLE mytable
(
id bigint NOT NULL,
sid bigint NOT NULL,
obs timestamp without time zone NOT NULL,
rcv timestamp without time zone NOT NULL,
uid bigint NOT NULL,
CONSTRAINT pkey PRIMARY KEY (id)
)
EOM
)
sudo -u postgres psql db -c $CMD
but that doesn't work because the command is now missing the quotes, and using "$CMD" doesn't remove the newlines so it is passed as multiline still...
however
echo $CMD
will print the command in a single line
so in the end using:
CMD=$(echo $(cat << EOM
CREATE TABLE mytable
(
id bigint NOT NULL,
sid bigint NOT NULL,
obs timestamp without time zone NOT NULL,
rcv timestamp without time zone NOT NULL,
uid bigint NOT NULL,
CONSTRAINT pkey PRIMARY KEY (id)
)
EOM
))
sudo -u postgres psql db -c "$CMD"
actually wraps the line in double quotes, into a single line command.
I hope this is useful to others.

- 19,584
- 12
- 87
- 105
-
-
1EOM is just a delimiter, but it can be anything you want. It is often EOF (end of file) or EOL (end of line). I think I used EOM for End of Macro. Anyway, whatever word you use, the only constraint is that the beginning and end match. – MrE Jun 02 '22 at 23:54
If i understand your requirement correctly, you can pass any thing in double quotes from command line i.e. "". It would be treated as a single argument and received in the program as a string, which can be parsed according to needs.
Here is a c program
#include <stdio.h>
int main(int c, char *argv[])
{
printf("argument is %s\n", argv[1]);
return 0;
}
On console, i compiled and ran it with
$ gcc args.c
$ ./a.out "<img src=\"image.png\" onload='alert(/loaded/)' />"
Output is
argument is
<img src="image.png" onload='alert(/loaded/)' />
For special characters such as "" (double quotes), backslash \ etc. inside the argument, just prefix those with an extra escape sequence \

- 5,412
- 4
- 28
- 68
-
-
Yes it will. I just verified through a program. Will post it in the solution. Done – fkl Sep 25 '12 at 06:10
-
2There are a number of characters that need to be escaped when they occur inside a double-quoted string, including double-quotes themselves. Note that the string printed out without quotes around `image.png`. If you ran it as `./a.out "
"` the double-quotes would be included in the argument. – Gordon Davisson Sep 25 '12 at 06:17
-
Yeah i missed that. For every special character an escape sequence should be added. Doubles quotes, backslash and other similar ones. Added that to answer. Thanks – fkl Sep 25 '12 at 06:21
-
I was already aware of using double quotes and escaping them in the given string. But how can do it without altering the original string? – Cookies Sep 25 '12 at 06:25
-
1Well you have to specify to the program a way to interpret characters. There is no special encoding here. So i don't think there is any way. Why would you need one? – fkl Sep 25 '12 at 06:36
-
Thanks. yea am trying to invoke nodejs from lua and pass a given html content available inside the lua context. – Cookies Sep 25 '12 at 07:19
-
So before passing html content, can't you run a linear scan on the string and insert a \ whenever you see one of special characters? – fkl Sep 25 '12 at 07:24
-
1Also note that newlines are treated differently depending on the shell. In csh and csh variants, newlines are special characters and must be escaped, while in bourne shells the `\` preceding a newline is optional. – William Pursell Sep 25 '12 at 11:53
This worked for me.
Here's the multiline text in a file (setup.json
), with internal double quotes for example:
{
"fieldA": "foo"
}
and here's how to send it as a single argument:
setup=$(cat setup.json)
./my_util "$setup"
Use the c program (other answer) from @fayyazkl to test it.

- 19,989
- 5
- 106
- 123