How To Escape Single and Double Quotes in Perl

## to add a single backslash in front of whatever char was matched
1 2 3 4 |
my $password = q#abc'A1r%Fgt&jh[-#; $password =~ s/(['])/\\$1/g; print qq#'$password'\n#; 'abc\'A1r%Fgt&jh[-' |
## to double whatever char was matched
1 2 3 4 |
my $password = q#abc'A1r%Fgt&jh[-#; $password =~ s/(['])/$1$1/g; print qq#'$password'\n#; 'abc''A1r%Fgt&jh[-' |
## to convert ‘ to ‘\” for shell execution
1 2 3 4 |
my $password = q#abc'A1r%Fgt&jh[-#; $passwordShell =~ s/(['])/$1\\$1$1/g; print qq#'$password'\n#; 'abc'\''A1r%Fgt&jh[-' |
Leave Your Comment
All fields marked with "*" are required.