问题
alex@d120432:~$ echo $0
bash
alex@d120432:~$ perl -e 'print "\x41\x42\x43\x44\x0a"x2'
ABCD
ABCD
alex@d120432:~$ i=2
alex@d120432:~$ echo $i
2
alex@d120432:~$ perl -e 'print "\x41\x42\x43\x44\x0a" x $i'
alex@d120432:~$
Is it possible to get the same output in the second perl
command? I cannot find correct syntax to use i
as repeat count.
回答1:
You will have to escape inner double quotes and use double quotes in shell for variable expansion:
perl -e "print \"\x41\x42\x43\x44\x0a\" x $i"
ABCD
ABCD
Following variant may also work:
perl -e 'print "\x41\x42\x43\x44\x0a" x '$i
回答2:
While Perl uses the double-quote for interpolation, just like the shell, Perl provides the qq{}
alternative.
The {}
delimiters may be any pair you wish. Using this construct can eliminate the need to escape quote characters in the situation described; for example:
perl -e "print qq(\x41\x42\x43\x44\x0a) x $i"
来源:https://stackoverflow.com/questions/60074869/perl-and-bash-variable-substitution-with-hexadecimal-characters-and-repeat-coun