0

Why is it that when you run the substitution commands, the order of execution is broken, as in the example below. First the chmod command was executed, then echo 1 and echo 2?

echo $(echo 1; echo 2; chmod 444 nonexistent_file)

Result:

chmod: Cannot access 'nonexistent_file': No such file or directory
1 2
1

1 Answer 1

8

The order of execution isn’t broken, the output order isn’t what you expect.

echo 1 and echo 2 execute, writing their output to their standard output, which the shell accumulates for the command substitution. Then chmod executes, and outputs its error message to standard error, which goes straight to the terminal and thus appears first. Finally, the outer echo runs with the result of the command substitution, “1 2”.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .