Why is the result of adding a string and a number (interview question) a few days ago, I attended an interview and wrote a running result. The question is probably a string and a number are assigned to a variable, output variable: $ test & nbsp; linux & nbsp; + & nbsp; 6; echo & nbsp; $ test; after running the command, the result is actually the following number, but I still don't understand why. please give me some guidance. thank you. ------ Solution-why is the result of adding a number to a string? (interview question)
I took an interview a few days ago and wrote a question and the running result. The question is probably a string with a number assigned to a variable, and the output variable is:
$ Test = 'Linux '+ 6;
Echo $ test;
Later, I ran the command and learned that the result is actually the following number, but I still don't understand why. please give me some guidance. thank you.
------ Solution --------------------
$ Test = 'Linux '+ 6;
You are doing addition!
In addition, strings must be converted to numbers before calculation.
Strings starting with a number can only be converted to 0.
That is:
$ Test = 'Linux '+ 6;
Equivalent
$ Test = 0 + 6;
So echo $ test; output 6