I want to use the echo command to show this output:

10 item in File

but seems that the below command does not work:

echo "$('cat file.txt | wc -l ') items in File"
  • What does it print, and why is that wrong? – roaima Jan 4 '17 at 19:16
  • Rewrite to: echo "$(cat file.txt | wc -l) items in File" ... it's because you are looking for a command defined as that inside the quotes – Echoes_86 Jan 4 '17 at 19:19

The correct command is:

echo "$(wc -l < file.txt) items in File"

Note the quotes, for example, by doing:

$('du / -h | sort -h')

You are telling to the shell "hey, execute 'du / -h | sort -h'", and bash will reply, "oh no, I don't know what is 'du / -h | sort -h' is". I am a not 100% sure but I think that when you quote something you escape special characters, like '|'.

The solution for the above example is remove the quotes.

$(du / -h | sort -h)

The output of your command already tells what is going on:

zsh: command not found: cat file.txt | wc -l
  • Also, $( wc -l <file.txt ), to remove useless use of cat. – Kusalananda Jan 4 '17 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.