Sample Header Ad - 728x90

command substitution and Dollar sign before variable

0 votes
1 answer
58 views
I have a file named fileWithOneCommand.txt with just one command as follows ps -aux|head -n 5 then I write a testing shell script named 'test5.sh' with content as follow: file=/home/somepath/fileWithOneCommand.txt $file; echo see; cat $file; echo see2; $(cat $file); echo see3; but I cannot understand the result, the result is as follow: $ ./test5.sh USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 168584 10308 ? Ss Feb19 0:49 /sbin/init splash root 2 0.0 0.0 0 0 ? S Feb19 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S Feb19 0:00 [pool_workqueue_release] root 4 0.0 0.0 0 0 ? I' or 'ps --help ' for additional help text. For more details see ps(1). see3 1) $file should show the content of variable file, so should be output ps -aux|head -n 5 but why the output is the **execution result** of ps -aux|head -n 5, not just show ps -aux|head -n 5? 2) cat $file; return ps -aux|head -n 5, but why
$(cat $file);
return error "error: user name does not exist"? As I google command substitution, it said "the output of a command replaces the command itself. Shell operates the expansion by executing a command and then replacing the command substitution with the standard output of the command." so for $(cat $file); inside the bracket, cat $file return ps -aux|head -n 5 so why $(cat $file); not return the execution result of ps -aux|head -n 5 but return an error "error: user name does not exist"?
Asked by user1169587 (133 rep)
Feb 20, 2025, 04:09 AM
Last activity: Feb 20, 2025, 09:31 AM