[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFE: a way to echo the arguments with quoting
From: |
Eduardo A . Bustamante López |
Subject: |
Re: RFE: a way to echo the arguments with quoting |
Date: |
Sat, 1 Mar 2014 16:02:07 -0800 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Sat, Mar 01, 2014 at 02:34:56PM -0800, Dave Yost wrote:
> I have an ugly function I wrote for zsh that does this:
>
> Sat 14:17:25 ip2 yost /Users/yost
> 1 634 Z% echo-quoted xyz \$foo 'a b c ' '\n'
> xyz '$foo' 'a b c ' '\n'
> Sat 14:17:53 ip2 yost /Users/yost
> 0 635 Z%
>
> It would be nice if there were an easy way to do this in bash.
>
> Here is my use case:
>
> echo-command() {
> echo -n 1>&2 "[ "
> echo-quoted -n 1>&2 $@
> echo 1>&2 " ]"
> }
>
> echodo() {
> echo-command $@
> eval "$@"
> }
>
> 1 652 Z% echodo sleep 1
> [ sleep 1 ]
> 0 653 Z%
>
> This is a bit of a hack because when I need to use a pipe character, for
> example, I have to quote it, and that gets echoed in a way that’s wrong for
> this purpose.
>
> 0 656 Z% echodo echo abc \| sed 's,b,_,'
> [ echo abc '|' sed s,b,_, ]
> a_c
> 0 657 Z%
>
> A builtin that does my ‘echodo’ without having to escape command-line
> metacharacters is what I really want.
>
> Is there such a thing?
(1) This list is for bug reports. If you need help with bash
scripting, consider using the help-bash mailing list.
(2) It's easier to achieve this using the DEBUG trap:
| address@hidden:~$ trap 'echo "$BASH_COMMAND"' DEBUG
| address@hidden:~$ echo foo >/dev/null
| echo foo > /dev/null
| address@hidden:~$ echo hello
| echo hello
| hello
(3) If you need 'eval' to achieve something in the shell, you're most
likely doing it wrong: http://mywiki.wooledge.org/BashFAQ/048
(4) For more advanced discussion regarding how to log commands to be
executed:
http://mywiki.wooledge.org/BashFAQ/050#I_want_a_log_of_my_script.27s_actions
But using the DEBUG trap should suffice, it's really advanced. If you
need to propagate that trap to children, use set -T and read about
shopt -s extdebug
Also, again, use help-bash for scripting questions.
--
Eduardo Alan Bustamante López