# escape using quotemeta: say "\Q$dir\E"; # escape only those six characters plus "\\": $dir =~ s/(?=[][{~*?\\])/\\/g; say $dir;