Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 741009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:34:58+00:00 2026-05-14T08:34:58+00:00

I want to run some commands using the system() command, I do this way:

  • 0

I want to run some commands using the system() command, I do this way:

execute_command_error("trash-put '/home/$filename'");

Where execute_command_error will report if there was an error with whatever system command it ran. I know I could just unlink the file using Perl commands, but I want to delete stuff using trash-put as it’s a type of recycling program.

My problem is that $filename will sometimes have apostrophes, quotes, and other weird characters in it that mess up the system command or Perl itself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:34:59+00:00Added an answer on May 14, 2026 at 8:34 am

    Generate the command name and arguments as an array, and pass that to system:

    my(@command) = ("trash-put", 'home/$filename');
    system @command;
    

    This means that Perl does not invoke the shell to do any metacharacter expansion (or I/O redirection, or command piping, or …). It does mean it does exactly what you told it to do.

    sub execute_command_error
    {
        system @_;
    }
    

    Borrowing information from the copious collection of comments:

    Which is clearly documented in perldoc -f system or at perldoc.perl.org/functions/system.html (@Ether).

    (See also the discussion of ‘exec’ below which is closely related.)

    Did you mean to put $filename in single quotes? (@mobrule).

    I did intend to use single quotes – I’m demonstrating that the $filename does not get expanded by Perl or Shell…In my test script, I used ‘my.$file‘, and that gave me a file with a $ in the name – as I intended.

    I think the desired quoting if you do want to invoke the shell (for example if you want some piping) is $command_line = “\”$command\” \”$arg1\” \”$arg2\”…”. (@Jefromi).

    Adding double quotes around the arguments won’t help with embedded $, backtick1, ‘$(...)‘ and related notations. You more nearly need single quotes around things, but then you need to rewrite embedded single quotes as “'\''” which generates a single quote to terminate the current single-quoted argument, a backslash-quote combination to represent a single quote, and another single quote to resume the single-quoted argument.

    This would be a good solution if I used system command directly; however I am using webmin’s execute_command function, which is a bit over my head so I wouldn’t know how to edit it to allow for arrays. Could you expand on the rewrite of embedded single quotes as “'\''“…This is what I will use, for now. (@Brian)

    Roughly speaking, the way the (Unix) shells treat single quotes is “everything from the first single quote up to the next is literal text, no metacharacters”. So, to get the shell to treat something as literal text, enclose it in single characters. That deals with everything except single quotes themselves. As my comment says, you have to use the 4-character replacement string to get a single quote embedded into the middle of a single quoted argument.

    There is probably a neater way to do it than this (using one or two map operations, perhaps), but this should work:

    for (my $i = 0; $i < scalar(@command); $i++)
    {
        $command[$i] =~ s/'/'\\''/g; # Replace single quotes by the magic sequence
        $command[$i] = "'$command[$i]'"; # Wrap value in single quotes
    }
    

    You can then join the array to make a single string for transmission to execute_command.


    It’s better to write that as system { $command[0] } @command to handle the case where @command has one element. This is one of the things I talk about in the “Secure Programming Techniques” chapter of Mastering Perl. (@briandfoy).

    As a general rule, I’ll accept this correction. I’m not sure it is crucial in this instance, though, where the command name is provided by the program and it is only the arguments that are possibly provided the user. The command name ‘trash-put’ is safe from shell expansions (IFS is reset to default by the shell when it starts, so that avenue of attack is not available).

    This issue is discussed in the ‘perldoc -f exec’ man page:

    If you don’t really want to execute the first argument, but want to lie to the program you are executing about its own name, you can specify the program you actually want to run as an “indirect object” (without a comma) in front of the LIST. (This always forces interpretation of the LIST as a multivalued list, even if there is only a single scalar in the list.)

    Example:

       $shell = '/bin/csh';
       exec $shell '-sh'; # pretend it's a login shell
    

    or, more directly,

       exec {'/bin/csh'} '-sh'; # pretend it's a login shell
    

    When the arguments get executed via the system shell, results are subject to its quirks and capabilities. See “STRING” in perlop for details.

    Using an indirect object with exec or system is also more secure. This usage (which also works fine with system()) forces interpretation of the arguments as a multivalued list, even if the list had just one argument. That way you’re safe from the shell expanding wildcards or splitting up words with whitespace in them.

       @args = ( "echo surprise" );
       exec @args;              # subject to shell escapes
                                # if @args == 1
       exec { $args[0] } @args; # safe even with one-arg list
    

    The first version, the one without the indirect object, ran the echo program, passing it “surprise” an argument. The second version didn’t; it tried to run a program named “echo surprise”, didn’t find it, and set $? to a non-zero value indicating failure.


    1 How do you get a back-tick to display in Markdown?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 420k
  • Answers 420k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check post_max_size as well. File data is sent as POST… May 15, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer All you need to do is submit the form using… May 15, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer It turns out that I was just being stupid and… May 15, 2026 at 10:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.