I have come across an oddity when performing a simple echo command. Can anyone explain what's going on? Here's the scenario, there are exactly three files in a folder and I want to replace their contents with a blank character. The files are:
ev_tracker.css ev_tracker.html ev_tracker.jsI tried a simple command to echo a space character to all files
$ echo \ > *and I got the following error:
bash: *: ambiguous redirectSo, I tried to be more specific…
$ echo \ > ev_tracker.*
bash: ev_tracker.*: ambiguous redirectAnd more specific still…
$ echo \ > ev_tracker.{css,html,js}
bash: ev_tracker.{css,html,js}: ambiguous redirectFinally, I performed the action on each file, individually, without error.
$ echo \ > ev_tracker.css
$ echo \ > ev_tracker.html
$ echo \ > ev_tracker.js
$Can anyone explain why I received the error? I'm using Ubuntu 14.04 and whatever default sh variant that it would have.
2 Answers
echo a > *will be expanded by bash to
echo a > ev_tracker.css ev_tracker.html ev_tracker.jsaccording to man bash (REDIRECTION)
The word following the redirection operator in the following descriptions, unless otherwise noted, is subjected to (...) pathname expansion (...)
If it expands to more than one word, bash reports an error.
you can use
echo a | tee * > /dev/nullsee man tee, tee command is designed to do what you are looking for.
note also that
echo \ | tee * > /dev/nullwill not output a backslash to files.
1You simply can do this:
$ echo ' ' > ev_tracker*According to the comment below, this is what I've done.
$ touch bla blaa blaaa
$ echo ' 1' > bla*
$ cat bla 1
$ cat blaa 1
$ cat blaaa 1It is working with only a space too, but that's difficult to show.
1