Perl escaping argument for bash execution

前端 未结 5 1544
感动是毒
感动是毒 2020-12-21 11:16

I\'ve written some code in Perl which executes some bash command within its execution. My problem was when bash command attributes contained white space inside which failed

5条回答
  •  星月不相逢
    2020-12-21 11:33

    Use quotemeta:

    my $arg1 = quotemeta('string to be escaped');
    `echo $arg1`
    

    Or \Q\E (which is exactly what quotemeta is);

    my $arg1 = 'string to be escaped';
    `echo \Q$arg1\E`
    

    And also please note that using echo is a bad way to print arbitrary strings.

    And do NOT place any quotes around parameters if you're using quotemeta.

提交回复
热议问题