简体   繁体   English

为什么这个Java代码打印两个参数?

[英]Why does this Java code print two arguments?

The below Java code prints two arguments when I pass !clear as input as shown below. 当我传递!clear作为输入时,下面的Java代码打印两个参数,如下所示。

class Test{

    public static void main(final String... arguments){
        for(String argument : arguments){
            System.out.println(argument);
        }
    }

}

Output: 输出:

$ java Test !clear
java Test clear
clear

UPDATE UPDATE

Using any linux command in place of clear produces the same result. 使用任何linux命令代替clear产生相同的结果。

$ java Test !pwd
java Test pwd
pwd

$ java Test !ls
java Test ls
ls

I am aware that this has got something to do with history expansion in bash and I can escape the ! 我知道这与bash中的历史扩展有关,我可以逃脱! character to solve this issue. 解决这个问题的角色。

However, I am curious to know the exact reason why this code prints the above two lines as output. 但是,我很想知道此代码将上述两行打印为输出的确切原因。 Could somebody please explain? 有人可以解释一下吗?

You are working within a Unix/Linux shell, probably bash . 您正在Unix / Linux shell中工作,可能是bash The ! ! character is a special character in the Unix shell, and thus your command is considered a history expansion. character是Unix shell中的特殊字符,因此您的命令被视为历史扩展。

In history expansion, the usual thing that happens is that the shell shows you what the command expands to. 在历史扩展中,通常发生的事情是shell会向您显示命令扩展到的内容。 The expansion of !clear will simply be clear provided that you had the command clear in your history , and the expanded command will therefore be java Test clear . 的扩张!clear将简单地clear 规定,您必须在命令clear在你的历史 ,因此扩展命令将java Test clear The shell then runs the command. 然后shell 运行命令。 Java runs and only sees one argument - clear , and that's what it prints. Java运行并且只看到一个参数 - clear ,这就是它打印的内容。

If instead, you run 相反,如果你运行

java Test '!clear'

(With the single quotes!) (用单引号!)

Java will get the argument !clear as is, without history expansion. Java将获得参数!clear ,没有历史扩展。

If you use a word that is not in the shell's history of commands, you'll get an "event not found error". 如果您使用的单词不在shell的命令历史记录中,您将收到“未找到事件错误”。 If you use a word that, in your shell history, started a command with arguments, the entire command with all its arguments will be substituted. 如果你使用一个单词,在你的shell历史中,用一个参数启动一个命令,整个命令及其所有参数都将被替换。 This is all part of bash and not Java. 这是bash一部分,而不是Java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 Java 中使用 Println 或 Print 会影响代码的执行顺序? - Why does in Java using Println or Print affect the order of execution of code? 为什么Java中的这段代码会打印引用而不是调用方法的结果? - Why does this code in java print a reference instead the result of invoking the method? 为什么以下代码显示“ 0 1 2 3”? - Why does the following code print “0 1 2 3”? java-如果布尔值为true但布尔值声明为false时,为什么此代码会打印应该打印的输出? - java - why does this code print the output it is supposed to print if a Boolean is true but the Boolean was declared to false? 为什么下面的简单 Java 代码块(打印 Hello World )会引发编译错误? - Why does the following block of simple Java code (print Hello World ) raise compilation error? 为什么我的java代码总是在android studio中打印出零信号强度 - Why does my java code always print out zero for signal strength in android studio 为什么此代码不打印应有的内容? - Why this code does not print what it should? 为什么以下代码打印为空? - Why does the following code print null? 为什么这段代码打印100而不是1? - Why does this code print 100 instead of 1? 为什么代码打印语句两次,但不同? - Why does the code print the statement twice, but differently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM