Perl (Scripting) - Escape literal dollar sign $

by
Jeremy Canfield |
Updated: February 08 2022
| Perl (Scripting) articles
Let's say you have the following Perl script which prompts a user for input.
#!/usr/bin/perl
use strict;
use warnings;
print "password: ";
my $password = <STDIN>;
chomp $password;
Running this script will produce the prompt. Let's say the use enters a value that contains the dollar sign $ character.
~]# perl example.pl
password: its$secret
At this point, the $password variable will contain the dollar sign character. For example, here is how you can output the value of the $password variable.
print "password = $password \n";
Which should output the following.
password = its$secret
Here is how you could update the $password variable so that the dollar sign character is escaped.
$password = s|\$|\\\$|g;
Did you find this article helpful?
If so, consider buying me a coffee over at