FreeKB - Perl (Scripting) ( and ) characters
Perl (Scripting) - ( and ) characters

Sometimes, the ( and ) characters can be problematic. First let's consider the following markup. This will print the text Hello(World) without issue. In other words, the ( and ) characters do not create any problems.

my $foo = "Hello(World)";
print "$foo\n";


On the other hand, let's say you are making an SSH connection to another server and checking if a file that contains the ( and ) characters exists.


use strict;
use warnings;

my ($stdout) = `ssh johndoe\ ls "/path/to/example(file).txt";


This will produce the following standard error.

sh: -c: line 0: syntax error near unexpected token `('


The easiest fix is to place a single quote or double quote around the command being issued, like this:

my ($stdout) = `ssh johndoe\ 'ls "/path/to/example(file).txt"';


A more convoluted fix would be to escape the ( and ) characters. However, this is certainly not ideal, as there may be other special characters that would also need to be escaped, so if you are going to use this method, you are going to eventually end up with a long list of special characters being escaped.

if ( $foo =~ '\(' or $foo =~ '\)' ) {
  $foo =~ s|\(|\\(|g;
  $foo =~ s|\)|\\)|g; 


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 7764f in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |