help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] pass command to script


From: Christof Warlich
Subject: Re: [Help-bash] pass command to script
Date: Fri, 26 Apr 2019 21:35:36 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

Greg Wooledge wrote:
Again, $[...] is taken.

Oh, sorry, for whatever reason, your posts did not show up on my other
mail account that I used today during daytime.

What you're describing is exactly how {...} quoting works in Tcl.  Maybe
what you really want is a better programming language than bash.

Unfortunately, changing the scripting language wouldn't help as long as
bash is used as the command line interpreter. The (unrecoverable) damage
to the command line will already be done, because bash's restrictive
quoting capabilities w.r.t. nesting prevents to build command lines that
may either be executed right away or (with just an additional wrapping
quote as suggested) after being passed to (and evaluated by) a (bash or
Tcl) script.

May I again give an example?! Let's say we have a (bash or Tcl or
whatever) script called foo that (in addition to some other work) is
supposed to evaluate and execute a (sequence of) commands passed as
command line parameter. In bash, such a script might look like this:

#!/bin/bash
echo "do something"
eval "$@"
echo "do something else"

And now let's pick some example command that's just complicated enough
to illustrate my point, e.g.

$ bar="hi     ho" bash -c $'echo "$bar"'
hi     ho

If I would like pass this command to my foo script, I need to do two things:

1) I need to wrap the entire command into quotes.
2) I need to properly escape the quoting that is already part of that
command.

Thus, we would have to do this:

$ ./foo $'bar="hi     ho" bash -c $\'echo "$bar"\''
do something
hi     ho
do something else

On the other hand, let's assume that bash knows nestable quotes, maybe
$<...>? Using these quotes, our command would look like this:

$ bar="hi     ho" bash -c $<echo "$bar">
hi     ho

But now, we would just only have to add the additional quoting wrapper
to allow passing that command to the foo script, i.e. no escaping at all
is required, because bash could now keep track of the quoting level
without the need of escaping:

$ ./foo $<bar="hi     ho" bash -c $<echo "$bar">>
do something
hi     ho
do something else

Finally note that both approaches allow infinite deep nesting, but while
the first one quickly becomes unreadable due to the required escaping,
the latter always only needs just one enclosing quote wrapper!

I hope that I finally managed to make my point clear :-).







reply via email to

[Prev in Thread] Current Thread [Next in Thread]