Why does Scala ignore exclamation points in command line arguments?

余生颓废 提交于 2019-12-25 04:23:02

问题


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:


! 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


来源:https://stackoverflow.com/questions/28891832/why-does-scala-ignore-exclamation-points-in-command-line-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!