So doing
$(cat /etc/passwd)
results in "No such File or Directory"
and
"$(whereis cat)"
results in "command not found"
Why is command substitution not working in the expected manner?
So doing
$(cat /etc/passwd)
results in "No such File or Directory"
and
"$(whereis cat)"
results in "command not found"
Why is command substitution not working in the expected manner?
TL;DR: You may be using command substitution the wrong way.
While you do not provide the full error message, it is very likely that the command substitution works exactly the way it is intended.
Command substitution means that the output of a command is put in the place where the command substitution is specified. That means if you have a script with a line
$(cat /etc/passwd)
the script will try to execute the content of the file /etc/passwd, as printed by cat, which consists of :-separated fields like
root:x:0:0:root:/root:/bin/ash
which of course is not a valid command.
Similarly, if you have a line
$(whereis cat)
in your script, it will try to execute the "command"
cat: /bin/cat /usr/share/man/man1/cat.1.gz
(or similar, depending on your distribution) which will fail because there is no command cat: on your system. This is aggravated by your use of " ... "-quoting - usually the right thing to do - which makes the shell treat the entire line (including spaces) as one command name that it tries to execute.
users="$(cat /etc/passwd)". Shell substitutes the contents of $() with its output, both in variable assignment and in OP's example; the difference is that one is a command invocation, while the other is specifically a variable assignment.