It is essentially a generic syntax error, not specifically related to the ls
token. bash
uses a yacc parser, which calls a common yyerror()
on any problem, Within the resulting error-handling, it proceeds to try to pinpoint the error. The message is coming from this chunk (see source):
/* If the line of input we're reading is not null, try to find the
objectionable token. First, try to figure out what token the
parser's complaining about by looking at current_token. */
if (current_token != 0 && EOF_Reached == 0 && (msg = error_token_from_token (current_token)))
{
if (ansic_shouldquote (msg))
{
p = ansic_quote (msg, 0, NULL);
free (msg);
msg = p;
}
parser_error (line_number, _("syntax error near unexpected token `%s'"), msg);
free (msg);
if (interactive == 0)
print_offending_line ();
last_command_exit_value = parse_and_execute_level ? EX_BADSYNTAX : EX_BADUSAGE;
return;
}
In other words, it's already confused by the '('
, and having looked-ahead for context is reporting the ls
.
A (
would be legal at the beginning of a command, but not embedded. Per manual page:
Compound Commands
A compound command is one of the following:
(list) list is executed in a subshell environment (see COMMAND EXECU‐
TION ENVIRONMENT below). Variable assignments and builtin com‐
mands that affect the shell's environment do not remain in
effect after the command completes. The return status is the
exit status of list.
Further reading:
#echo $(ls)
– Ijaz Ahmad Dec 27 '15 at 18:28(ls) | xargs echo
– jlliagre Dec 27 '15 at 19:59echo (ls)
do? Generate an error. What doesecho $(ls)
do? Pretty much the same thing asls
, making theecho
and command substitution redundant. If you're using it for testing, stop: don't try to parsels
output. – Blacklight Shining Dec 31 '15 at 10:38