2
[Running Java 1.7.0_51 on MacOSX]

Hey there,

This is frustrating me, as I've just successfully finished a piece of work I was supposed to finish in Java. However I'm getting some errors, when I run the code, for example,
if I compile it and use: java MyCode "Hello World!"

I get the error: -bash: !": event not found

However, if I use single quotes like 'Hello World!', the program runs without any issues, or if the exclamation mark is not on the end if using the double quotes still.

I know this might not be directly a coding question, but I wasn't too sure, so I hope someone who may have had issues when running something similar, knows about this.

Thanks!

zahz
  • 37
  • 6

1 Answers1

2

Right way to run your code from BASH is indeed by using single quotes:

java MyCode 'Hello World!'

In double quotes shell expands the quoted string and tries to find an event from history following ! mark.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Alright. This was just an assessed piece of work, so I was worried if this was an issue, I'd get zero. They'll know this already then I suppose? Thanks! – zahz Mar 31 '14 at 17:14
  • @zahz - If they don't then everyone will get zero. Does that seem likely to you? – Stephen C Mar 31 '14 at 17:16
  • Sorry for my newbieness lol. I genuinely didn't know this. Thanks guys, will mark as best answer. – zahz Mar 31 '14 at 17:17
  • @zahz Don't worry; it's a consequence of how `bash` works, not anything to do with your code. `java MyCode "Hello World!"` works as expected from the `dash` shell, for example, because that shell does not have history expansion. – chepner Mar 31 '14 at 17:18