Data truncation when '$' or '&' is used as argument in java command line

Rishi :

I am getting data truncation when I pass '$' or '&' as part of data in my java arguments. I know that issue can be resolved by using escape character '\' before '$' but I don't understand why we are seeing this only for '$' or '&' and not for any other special characters like '@','#','%','^' or'*'

Following is my standalone test case to reproduce this error

import java.util.Arrays;
public class Main{

    public static void main(String [] args)
    {
        System.out.println(Arrays.toString(args));
        System.out.println(args[0]);
    }
}

Input

java Main name=Daniel$Burgers

Output

[name=Daniel]
name=Daniel
Jon Skeet :

This isn't a Java issue - it's your shell; the command line from which you're running the code. $ is usually used for variable substitution in shells; & is usually used for background processes. You'd see the same using echo:

$ echo name=Daniel$Burgers

Output:

name=Daniel

If you put the value in single quotes, that tells the shell not to do anything special until the end of the string (another single quote):

$ echo 'name=Daniel$Burgers'

Output:

name=Daniel$Burgers

The same change should fix your Java invocation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23138&siteId=1