Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash echo executes command instead of printing it

I have simple bash command that I need to put in shell:

 `for f in $(ls); do echo "File -> $f"; done`

What I get is:

-bash: File: command not found

I don't understand why bash is trying to execute echo statement instead of printing it...

like image 994
mnowotka Avatar asked Dec 04 '25 07:12

mnowotka


2 Answers

The backticks cause the execution. The command inside the backtiks outputs a string, and the backticks execute that string as a command.

like image 193
William Pursell Avatar answered Dec 07 '25 02:12

William Pursell


You should'nt be really parsing ls for this. Do something like:

for f in *; do echo "File -> $f"; done

For directories:

for i in *; do if [ -d $i ]; then  echo "File -> $i"; fi ; done

or

find . -type d -exec echo '{}' \;
like image 36
jaypal singh Avatar answered Dec 07 '25 02:12

jaypal singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!