Note that there are some explanatory texts on larger screens.

plurals
  1. POGet last bash command including pipes
    text
    copied!<p>I wrote a script that's retrieving the currently run command using $BASH_COMMAND. The script is basically doing some logic to figure out current command and file being opened for each tmux session. Everything works great, except when user runs a piped command (i.e. <code>cat file | less</code>), in which case $BASH_COMMAND only seems to store the first command before the pipe. As a result, instead of showing the command as <code>less[file]</code> (which is the actual program that has the file open), the script outputs it as <code>cat[file]</code>.</p> <p>One alternative I tried using is relying on <code>history 1</code> instead of $BASH_COMMAND. There are a couple issues with this alternative as well. First, it does not auto-expand aliases, like $BASH_COMMAND does, which in some cases could cause the script to get confused (for example, if I tell it to ignore <code>ls</code>, but use <code>ll</code> instead (mapped to <code>ls -l</code>), the script will not ignore the command, processing it anyway), and including extra conditionals for each alias doesn't seem like a clean solution. The second problem is that I'm using HISTIGNORE to filter out some common commands, which I still want the script to be aware of, using history will just make the script ignore the last command unless it's tracked by history.</p> <p>I also tried using <code>${#PIPESTATUS[@]}</code> to see if the array length is 1 (no pipes) or higher (pipes used, in which case I would retrieve the history instead), but it seems to always only be aware of 1 command as well.</p> <p>Is anyone aware of other alternatives that could work for me (such as another variable that would store $BASH_COMMAND for the other subcalls that are to be executed after the current subcall is complete, or some way to be aware if the pipe was used in the last command)?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload