Free LPI 102-500 Actual Exam Questions - Question 14 Discussion
function myfunction { echo $1 $2 ; }
in Bash, which output does:
myfunction A B C
Produce?
D imo, the key is it only echoes $1 and $2, so anything after B is ignored. The missing () might be weird, but output definitely won’t include C, ruling out options with three arguments printed.
I’m curious if the missing () after myfunction affects how arguments are passed. If it does run, $1 and $2 should only be A and B, so output matches option A. Does anyone know if that syntax is valid in all Bash versions?
I’m thinking the key here is how Bash assigns positional parameters inside the function. Since the function just echoes $1 and $2, it should only print the first two arguments given when called. The extra argument C is basically ignored because it’s not referenced. The syntax without parentheses is allowed in Bash for functions, so that part shouldn’t mess with arguments either. So, it seems like the output would be the first two arguments separated by space, matching option A. Does anyone see a reason why the extra argument C would get included in the output?
It’s A because the function just prints the first two arguments, ignoring any extra ones like C. The missing parentheses don’t affect how $1 and $2 are handled here.
Maybe A since the function just echoes $1 and $2, so only the first two arguments matter. The extra argument C should be ignored in the output.
Maybe D but thinking about it again, the function only echoes $1 and $2, so it should print the first two arguments given. The call is myfunction A B C, so $1 is A and $2 is B. That means it outputs “A B,” which matches option A, not D. So probably best to go with A because options with C or any different ordering don't make sense given just $1 and $2 are echoed.
Yeah, the key here is the function only uses $1 and $2, so any arguments beyond those don’t get printed. That rules out B, C, D, and E since they all include C or reorder the arguments. So it has to be A. The function call myfunction A B C just echoes “A B” and ignores the third argument completely.
Option A makes the most sense here. The function is defined to echo just $1 and $2, so when calling myfunction A B C, it should only output “A B.” The extra argument C isn’t used or printed, so any option showing C in the output can be thrown out. Also, since it’s a Bash function, it should accept arguments as expected, even if the syntax looks a bit off in the question. So yeah, option A fits best with how Bash functions handle positional parameters.
Not B, since the function only echoes $1 and $2, extra arguments like C won’t appear in output. So it can’t be printing all three arguments.
A, because the function prints only the first two arguments given.
A The function only prints the first two arguments, so C is ignored. No reason to pick anything else since none show just A and B.
Assuming the function runs despite missing parentheses, it only echoes $1 and $2, so the third argument C is ignored. That rules out B, C, D, and E since they all involve the third argument somehow. But I'm wondering if the function syntax without parentheses would actually run in all Bash versions or just newer ones? Could this be a trick question testing syntax validity rather than output?
A/B? I get why A is popular, but technically without parentheses, the function might not work in strict Bash. If it does, it should be A. Otherwise, it might error out.
Option A, since only $1 and $2 show up, $3 is dropped.
Doesn't the function syntax need parentheses after the name? So maybe none output exactly as shown?
Maybe A. The function only echoes the first two positional parameters, so the third argument should just be ignored without error.
B imo, function syntax might accept all args, echoing A B C even if only $1 $2 used.
A. The function definitely just echoes $1 and $2, so it stops at "A B" and leaves out "C". The extra argument doesn’t get printed or cause an error—it’s just ignored here. The lack of parentheses in the function declaration might be odd stylistically, but Bash still accepts it.
It’s A because only $1 and $2 are called, so C is never printed.
A imo, since only $1 and $2 are echoed, the third argument is ignored.