diff --git a/Shell/AST.cpp b/Shell/AST.cpp index 121530c66a6..4bbf3777ba8 100644 --- a/Shell/AST.cpp +++ b/Shell/AST.cpp @@ -745,7 +745,7 @@ RefPtr Execute::run(RefPtr shell) auto initial_commands = m_command->run(shell)->resolve_as_commands(shell); decltype(initial_commands) commands; - for (auto& command : initial_commands) { + Function resolve_aliases_and_append = [&](auto& command) { if (!command.argv.is_empty()) { auto alias = shell->resolve_alias(command.argv[0]); if (!alias.is_null()) { @@ -757,15 +757,26 @@ RefPtr Execute::run(RefPtr shell) subcommand_ast = ast->command(); } RefPtr substitute = create(position(), move(subcommand_ast), create(position(), command)); - commands.append(substitute->run(shell)->resolve_as_commands(shell)); + for (auto& subst_command : substitute->run(shell)->resolve_as_commands(shell)) { + if (!subst_command.argv.is_empty() && subst_command.argv.first() == argv0) // Disallow an alias resolving to itself. + commands.append(subst_command); + else + resolve_aliases_and_append(subst_command); + } } else { commands.append(command); } } else { commands.append(command); } + } else { + commands.append(command); } - } + }; + + for (auto& command : initial_commands) + resolve_aliases_and_append(command); + Vector> jobs_to_wait_for; auto run_commands = [&](auto& commands) {