2

I have recently answered a question Should variables be quoted when executed? Basically, my answer stated that executing variables is a doomed technique, that no amount of quoting will cover all the edge cases, and the only real alternative is to use functions or eval. That answer was deleted on the spot.

Now, I can understand how this answer is unhelpful for the OP who have already decided to execute variables and wants to know how to use quoting to its best. But isn't it helpful to have it as a warning for newcomers who may consider that executing variables in bash is a perfectly valid approach, without even considering alternatives?

In case my answer is simply wrong, why did it get deleted instead of being downvoted?

  • also, you're wrong. you can quote the edge cases by quoting correctly. – mikeserv Dec 26 '15 at 18:11
  • That is entirely possible. I'm only opposed to the removal of my answer. Comment/downvote/edit as you see fit - I'd be glad to learn. – Dmitry Grigoryev Dec 29 '15 at 18:02
  • 1
    I did neither thing. I did answer it. But I do agree wholeheartedly with your sentiment. Keep writing them. Get 10k rep. There ought to be more close/open voters here w/ that sentiment if you ask me. – mikeserv Dec 29 '15 at 18:06
3

I deleted it because it wasn't actually an answer. Perhaps I should have let another mod do this since this was my own question but it seemed like a very clear-cut case. Your answer was:

What are you trying to achieve by executing variables? It's simply impossible to write a run_this function which correctly handles all the edge cases, like

  • pipelines
  • input/output redirections
  • shell statements like if while etc.

If you want to avoid code repetition, you're better off using functions. And if the commands are only available at run time, why not just use eval?

As you can see, it never actually answers a question. All it does is ask new ones. You give some valid advice, but none of it actually answers the question asked. Answers should only be posted if they provide an answer to the question. Yours seemed to be asking the OP (OK, me) why he chose to do something this way. That should have been in a comment. The question wasn't about any of the edge cases your answer addressed but about the simple ones asked in the question.

My question, foolish and born of a lack of caffeine though it was, was about the specific case of executing $@ inside a function. If your answer had been "Don't do that, it's silly", it wouldn't have been deleted. However, the answer you posted mostly consisted of more questions and offered no clear answer to the OP.

Next time, write your answer in a way that makes it clear that you're answering. For example:

It's simply impossible to write a run_this function which correctly handles all the edge cases, like

  • pipelines
  • input/output redirections
  • shell statements like if while etc.

If you want to avoid code repetition, you're better off using functions:

show an example of a function that does what the OP wnats

And if the commands are only available at run time, you can use eval:

show an example of using eval for what the OP wants

If you would like to post such an answer, you can either edit your original one and flag it for mod attention (I'll undelete it) or post a new one.

  • Thanks, I'll take your offer! (i.e. extend my answer and flag for undeletion when ready) – Dmitry Grigoryev Dec 23 '15 at 13:24
  • 1
    @DmitryGrigoryev cool, just flag or comment here when you're done. I can understand why that would have seemed kind of abrupt to you, sorry. I should have left you an explanatory comment. Just so you know, the question was prompted by muru's edit of my answer here which quite correctly added the quotes. I just had a PEBKAC moment and didn't think to test with an argument containing spaces. – terdon Dec 23 '15 at 13:26
  • 1
    @DmitryGrigoryev I just noticed that I had left you a comment. Could you please confirm that you were notified of the comment despite the question having been deleted? – terdon Dec 23 '15 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .