help-bash
[Top][All Lists]
Advanced

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

Re: feature request: built-in variable for the whole previous command in


From: Chet Ramey
Subject: Re: feature request: built-in variable for the whole previous command input
Date: Fri, 9 Jul 2021 10:37:43 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.11.0

On 7/9/21 4:36 AM, Ante Bilandzic wrote:
Hello,

There is a built-in variable $_ which retrieves the last word (argument) of
the previous command input. Would it be possible to add in the same fashion
a new built-in variable that retrieves the whole previous command input
(command + all its arguments)?

I don't think it's worth a new variable, especially given the existence of
BASH_COMMAND.

I was trying with 'echo !!' or 'fc -ln -1' but they seem to work only in an
interactive session in the terminal, and not when sourced in the script.

History expansion works in scripts -- that's how I test it -- but you have
to enable both history and history expansion.


Use case: We can then write very compactly and elegantly in the scripts
code snippets like this:

some-command-input || { echo $requested-new-built-in-variable; return 1; }

As others pointed out, you can use a DEBUG trap for this.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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