2

for example

$ gcc -Wall abc.c

$ ./a.out <font name="Moronicity" size=12><!-- ignore this comment --><i></i>
<div style="aa">hello</div></font><img src="spacer.gif">
<div style="bb"><img src="spacer.gif"></div>

-bash: syntax error near unexpected token `<'

Would keep getting this error

  • 4
    Not an answer, but: that's not typical on Unix. Long strings are passed to programs in files or via standard input. So if that's a program you're writing, don't do that is the answer. – derobert Feb 24 '18 at 1:30
  • Thanks @derobert... deleted the answer and deleting the comment too – koleygr Feb 24 '18 at 1:30
  • 1
    You put any text into a shell according to its syntax not the way you imagine or just want. RTFM – poige Feb 24 '18 at 14:22
6

You could use escape characters before each special character (<, [, >, ]), but that'd be quite cumbersome in this case. Instead, you can simply surround the entire argument with single quotes as follows:

$ ./a.out '<font name="Moronicity" size=12><!-- ignore this comment --><i></i>
<div style="aa">hello</div></font><img src="spacer.gif">
<div style="bb"><img src="spacer.gif"></div>'

Another option is to place the parameter string

<font name="Moronicity" size=12><!-- ignore this comment --><i></i>
<div style="aa">hello</div></font><img src="spacer.gif">
<div style="bb"><img src="spacer.gif"></div>

into a file (for example, params). This allows calling of your function in combination with the cat command, which outputs the contents of a file:

$ ./a.out "$(cat params)"

Note that the $() is used to execute the cat params command, and the double quotations are used to include the entirety of the file as the parameter to a.out. With the combination of the two, we can pass the contents of the file into the parameters of your program.

  • 3
    FYI, there's also the ./a.out "$(< params)" syntax. – Jeff Schaller Feb 24 '18 at 1:28
  • That syntax looks a bit cleaner. Thanks for the heads up. – aliceinpalth Feb 24 '18 at 1:30
11

If you need that HTML text to be a parameter to the program, then you need to quote it to protect it from the shell (which sees less-than signs as redirection, among other things):

./a.out '<font name="Moronicity" size=12><!-- ignore this comment --><i></i>
<div style="aa">hello</div></font><img src="spacer.gif">
<div style="bb"><img src="spacer.gif"></div>'

If you need that HTML text to be sent to the program as input (stdin), then you can quote it as a here-document. I've further indented the first line to indicate that the rest of the text all begins at column 1:

./a.out << 'EOF'
<font name="Moronicity" size=12><!-- ignore this comment --><i></i>
<div style="aa">hello</div></font><img src="spacer.gif">
<div style="bb"><img src="spacer.gif"></div>
EOF

The single-quotes around EOF prevent expansion of of any parameters in the text.

  • 3
    Also note that depending on the length of the HTML document, giving it as an operand on the command line may result in an "Argument list too long" error if it's too long. – Kusalananda Feb 24 '18 at 7:17

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.