I want to get user's command and execute it at some point.

Is it supposed to be a shell command (a single string which may contain shell-special character combinations like &&), or an executable followed by an argument list, to be executed by system PROGRAM LIST with no processing by the shell?

In the former case, any shell-special characters will be interpreted by the shell. In a way, you'd be introducing a shell injection vulnerability into your program. If the user passes types "echo hello world \`rm -rf whatever\`" as the command line argument, the string reaching the shell would be echo hello world `rm -rf whatever`, making the shell dutifully run rm -rf whatever before running echo hello world. Maybe that's what you want: after all, no security boundaries are crossed here, and the user is always able to rm -rf anything they want by typing the command instead of making your program do it.

The downside is having to quote anything that might contain spaces, dollar signs, single and double quotes and everything else. If you want to supply a file name I'm eating in bed.ogg to mpv, you have to first quote it "I'm eating in bed.ogg", then quote the quotes: "mpv \"I'm eating in bed.ogg\"". There, now it's safe to pass to your program -- if it's intended to use the shell to run the command lines. Note that you can't use single quotes here because of escaping rules, but you could if the file name didn't contain an apostrophe. Add a few layers of that ("I need to pass this file name to a program that runs a command line that runs another command line..."), and therein lies madness.

In the latter case, the arguments end up more or less directly passed to the execve system call, but they have to survive being an array of the arguments and not a single string. This way you can pass arbitrary NUL-terminated strings to the child process, no matter their contents, because a shell doesn't even come near them, so there's no danger in it interpreting `rm -rf whatever` and running it as a program, if you had a file named like that.


In reply to Re: @ARGV ignores quotes by Anonymous Monk
in thread @ARGV ignores quotes by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.