help-bash
[Top][All Lists]
Advanced

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

Re: Kludge for handling REPL arithmetic expressions


From: Tapani Tarvainen
Subject: Re: Kludge for handling REPL arithmetic expressions
Date: Tue, 3 Aug 2021 10:05:00 +0300

On Mon, Aug 02, 2021 at 08:32:10PM -0700, Eric Pruitt (eric.pruitt@gmail.com) 
wrote:

[...]
>     # Handler used to intercept arithmetic expressions so they can be
>     # entered without having to add a space after the "=".
>     #
>     function command_not_found_handle()
>     {
>         if [[ "$1" != =* ]]; then
>             printf "%s: command not found\n" "$1" >&2
>             return 127
>         fi
> 
>         eval "= ${1#=} ${@:2}"
>     }
>
> This works well until division comes into play:
> 
>     ~$ =2+2^7
>     130
>     ~$ =2/3
>     bash: =2/3: No such file or directory
>     (127)

Right: command_not_found_handle() is not called when there's a slash
in the command.

Offhand I can't any reason why it shouldn't, but making that happen
would mean modifying bash source code. A wishlist item, then.

> Is there any way I can work around this without modifying the Bash
> source code? I may regret saying this, but no kludge is too
> terrible.

One just-maybe-might-work idea:

Redefine = as a macro that inserts a function name in the beginning of
the line followed by space and return. Then have that function first
check if it was alone in the beginning of the line. In the beginning,
unbind = and replace itself with "= "; elsewhere unbind = and re-enter
the original line.

Then have another function called by $PS1 re-do the binding.

Not tested, I'm not at all sure it's possible or that it won't break
something horribly, and even if you can make it work it'll be a
terrible kludge, but...

-- 
Tapani Tarvainen



reply via email to

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