4

Trying to run a simple Perl script in a command window and am getting error:

string terminator "'" anywhere before EOF at -e line 1

Code

perl -e 'print "Hello World";'

What am I doing wrong?

Community
  • 1
  • 1
Eric
  • 103
  • 6
  • 1
    I just copy pasted and ran this, runs fine for me. – Lazer Dec 04 '10 at 05:03
  • If you need/want to run one-lines like that on Windows, I would suggest intsalling Cygwin: http://www.cygwin.com/ - it gives you a unix-like environment, including a proper shell (bash) and perl... – slu Dec 04 '10 at 07:29
  • Possible duplicate of *[Why doesn't my Perl one-liner work on Windows?](https://stackoverflow.com/questions/660624/why-doesnt-my-perl-one-liner-work-on-windows)* – Peter Mortensen Jul 12 '19 at 19:00

3 Answers3

12

Which platform? If it was Windows and CMD.EXE, then all sorts of things could be going wrong. On a Unix-like platform, that should work fine. No newline at the end, so it's likely your prompt would appear to start with 'Hello World', but that's all.


With the comment that it is Windows, then the trouble is that Windows CMD.EXE does not parse the command line the same as Unix, and you can't simply use single quotes around arguments; you have to use double quotes. Try:

perl -e "print qq{Hello World\n}"

There's a modest chance it will work for you.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
6

From perldoc perlfaq3 - Why don't Perl one-liners work on my DOS/Mac/VMS system?

The problem is usually that the command interpreters on those systems have rather different ideas about quoting than the Unix shells under which the one-liners were created. On some systems, you may have to change single-quotes to double ones, which you must NOT do on Unix or Plan9 systems. You might also have to change a single % to a %%. For example:

# Unix (including Mac OS X)
perl -e 'print "Hello world\n"'

# DOS, etc.
perl -e "print \"Hello world\n\""

# Mac Classic
print "Hello world\n"
 (then Run "Myscript" or Shift-Command-R)

# MPW
perl -e 'print "Hello world\n"'

# VMS
perl -e "print ""Hello world\n"""

The problem is that none of these examples are reliable: they depend on the command interpreter. Under Unix, the first two often work. Under DOS, it's entirely possible that neither works. If 4DOS was the command shell, you'd probably have better luck like this:

perl -e "print <Ctrl-x>"Hello world\n<Ctrl-x>""

Under the Mac, it depends which environment you are using. The MacPerl shell, or MPW, is much like Unix shells in its support for several quoting variants, except that it makes free use of the Mac's non-ASCII characters as control characters.

Using qq(), q(), and qx(), instead of "double quotes", 'single quotes', and backticks, may make one-liners easier to write. There is no general solution to all of this. It is a mess.

Zaid
  • 36,680
  • 16
  • 86
  • 155
0

Try: perl -e " print 'Hello..'; " This works in the Windows CMD.EXE console where quoting is not POSIX standardized.

G. Cito
  • 6,210
  • 3
  • 29
  • 42
Shailesh
  • 358
  • 2
  • 13