Linux command behaves strangely in shell command line Linux command behaves strangely in shell command line shell shell

Linux command behaves strangely in shell command line


What's happening is that $x is executed as a single command. The shell isn't "re-parsing" the contents of $x, and so it's not, in particular, setting up redirection and spawning sort.

What happens is that cat gets handed three arguments: file, | and sort. You don't have a file named | or sort in that directory, so cat can't open them. (I'm guessing that what the error says anyway1.)

You could get the result you expect by running:

eval "$x"

I'd avoid using eval if you can though, it can be dangerous.

1Prefix your commands with LC_ALL= to get English error messages before asking for help on international sites.
e.g. LC_ALL= $x.