0

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

I tried a simple command to echo a space character to all files

$ echo \  > *

and I got the following error:

bash: *: ambiguous redirect

So, I tried to be more specific…

$ echo \  > ev_tracker.*
bash: ev_tracker.*: ambiguous redirect

And more specific still…

$ echo \  > ev_tracker.{css,html,js}
bash: ev_tracker.{css,html,js}: ambiguous redirect

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

  • So you're echoing a backslash without escaping (so it'll be blank) to files so just echo that to each file individually rather than using wildcards and it should work. Otherwise, you may need to build a loop or an array to do it to all one-by-one that way with iterations. it seems it cannot figure out the one-by-one otherwise. – Formerly Pimp Juice IT Jan 4 '16 at 5:57
  • Your redirect output to a single destination. If you feed it three differents filesnames as a single destination then the shell will complain. (and I can not argue with it, 3 != 1). – Hennes Jan 4 '16 at 15:41
  • right on, @hennes, i was just perplexed that the > operation could not figure out that by using the wildcard i wanted the output sent to all three files. however, now that i know that it expects a single target i will be sure to loop such actions in the future, or use tee as mentioned below. – Madcarrots Jan 4 '16 at 18:51
3
echo a > *

will be expanded by bash to

 echo a > ev_tracker.css ev_tracker.html ev_tracker.js

according 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/null

see man tee, tee command is designed to do what you are looking for.

note also that

echo \ | tee * > /dev/null

will not output a backslash to files.

| improve this answer | |
  • thanks, @archemar, this seems to be the most informative answer; i was not aware that > could only be directed to one target. with regard to your note, i was sending a blank space to the files; i'm not sure if the posted question accurately displayed both space characters after the backslash in my original command. – Madcarrots Jan 4 '16 at 18:27
-2

You 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
 1

It is working with only a space too, but that's difficult to show.

| improve this answer | |
  • I've tested it on my machine it is working, I'm using arch-linux. – mstruebing Jan 4 '16 at 6:16

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.