Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this Java code print two arguments?

Tags:

java

linux

bash

The below Java code prints two arguments when I pass !clear as input as shown below.

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

Using any linux command in place of clear produces the same result.

$ 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 ! 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?

like image 220
Beginner Avatar asked Jul 12 '17 13:07

Beginner


People also ask

How do you pass multiple arguments to a method in Java?

One using overloaded method (one for each) and another put the arguments into an array, and then pass this array to the method. Both of them are potentially error-prone and require more code. The varargs feature offers a simpler, better option.

What is command line argument in Java?

Java Command Line Arguments. The java command-line argument is an argument i.e. passed at the time of running the java program. The arguments passed from the console can be received in the java program and it can be used as an input.

What is the use of console arguments in Java?

The arguments passed from the console can be received in the java program and it can be used as an input. So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.

Can the name of a method parameter be different from argument?

As you can see name can be different but type of argument and sequence should be the same as the parameter defined in the method declaration. Name given to the method parameter is used within the method body to refer to the passed-in argument. Though there is no restriction on the name of a method parameter, still it has to follow some rules.


1 Answers

You are working within a Unix/Linux shell, probably bash. The ! character is a special character in the Unix shell, and thus your command is considered a history expansion.

In history expansion, the usual thing that happens is that the shell shows you what the command expands to. 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. The shell then runs the command. Java runs and only sees one argument - clear, and that's what it prints.

If instead, you run

java Test '!clear'

(With the single quotes!)

Java will get the argument !clear as is, without history expansion.

If you use a word that is not in the shell's history of commands, you'll get an "event not found error". 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. This is all part of bash and not Java.

like image 109
RealSkeptic Avatar answered Sep 28 '22 12:09

RealSkeptic