Why does Scala ignore exclamation points in comman

2019-09-06 01:11发布

If I write a Scala script (or App) that just prints out the command line arguments, I notice that it ignores all the '!' characters. For example, if my script looks like:

println(args(0))

And I run it with:

scala MyScript "Hello!"

I get the output:

Hello

And if I run it with:

scala MyScript "Hello! World!"

I also get:

Hello

What's going on? What is the purpose of the ! character in arguments that causes this behaviour?

1条回答
Anthone
2楼-- · 2019-09-06 01:58

! is history substitution.

Try scala MyScript hello! with ! at EOL to see it work as if quoted.

http://www.gnu.org/software/bash/manual/bashref.html#Event-Designators

http://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes

On windows, the "scala.bat" command script has delayed expansion enabled.

http://en.wikibooks.org/wiki/Windows_Batch_Scripting

http://en.wikibooks.org/wiki/Windows_Batch_Scripting#SETLOCAL

http://en.wikibooks.org/wiki/Windows_Batch_Scripting#Command-line_arguments

To disable interpretation of !myvar!:

C:\Users\you> scala greeting.script "hello^!"
hello!

C:\Users\you> set world= Bob

C:\Users\you> scala greeting.script "hello!world!"
hello Bob
查看更多
登录 后发表回答