mnowotka
mnowotka

Reputation: 17218

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...

Upvotes: 2

Views: 3099

Answers (4)

David Foerster
David Foerster

Reputation: 1540

Why not $(find . -mindepth 1 -maxdepth 1 -type d ! -name index -printf '%f/* ')?

  • no looping shell code
  • no escaping issues on the input side
  • portable

Upvotes: 2

Ziyaddin Sadygly
Ziyaddin Sadygly

Reputation: 9636

I used it in sh mode, works fine. Remove the backsticks at the start and at the end of command, and it will work.

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77075

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 '{}' \;

Upvotes: 3

William Pursell
William Pursell

Reputation: 212198

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

Upvotes: 3

Related Questions