polluted $_ after using fc

#1 · closed · 6 comments

View on GitHub ↗

step-

Car you reproduce this? I can here. It seems that when fc is used to edit the command line (my EDITOR is vim) then $_ is polluted with 'show_command_in_title_bar', I guess from bashrc.title. ``` root@del: ~ # cd /tmp root@del: /tmp # touch A B root@del: /tmp # fc mkdir -p /tmp/test show_command_in_title_bar [11s, exited with 1] root@del: /tmp # mv A B $_/ mv: target ‘show_command_in_title_bar/’ is not a directory [exited with 1] root@del: /tmp # ``` Versions -- I'm running a LinuxFromScratch 7.5 OS ``` root@del: /tmp # bash --version GNU bash, version 4.2.53(2)-release (x86_64-unknown-linux-gnu) ```

Comments

mgedmin

Yes, my bash prompt unfortunately destroys $\_. I don't know if that can be fixed somehow -- can you manually assign to $\_ in bash? I've learned to use Alt-. instead -- it's a readline binding that inserts the last argument of the previous command. As a bonus, it lets me see the actual value that will be used in the command before I press Enter.

step-

I think you can assign `_=value` but it shadows the builtin variable. Is your script _designed_ to destroy `$_` or is it by accident? Do you know which lines destroy `$_`? So you're saying that your bash prompt always destroys `$_`. I thought using `fc` was a pre-condition. I'm not sure about switching to Alt-, I've never felt confortable using Alt/Ctrl key mappings on the command line. But it _is_ a work-around, I agree. Thanks.

mgedmin

I believe it's `trap show_command_in_title_bar DEBUG` makes bash overwrite `$_` after every command, because somehow bash doesn't distinguish commands entered on the command line and commands executed via the DEBUG trap. (I think it's a bug in bash, but I never found the time to report it.) It's not necessary to use `fc`: ``` mg@platonas: ~ $ echo foo foo mg@platonas: ~ $ echo $_ show_command_in_title_bar ``` I tried assigning to `_`, but it didn't seem to work: ``` mg@platonas: ~ $ _=foo; echo $_ show_command_in_title_bar ```

step-

I see. Then you could work around this bash issue by sub-shelling: ``` sh # Do some work for bashrc.prompt update_cmdstart } trap '( show_command_in_title_bar )' DEBUG else # Do some work for bashrc.prompt trap '( update_cmdstart )' DEBUG fi ``` It's a small time penalty for getting back `$_`. I think it's worth it. I tested it only briefly with ``` read x # press Enter echo $_ # echoes 'x' ``` This change introduces a smaller issue, string "&0" is inserted just before "#" in the prompt. I didn't look at how to fix that, I can live with it.

mgedmin

I have found a simpler fix. Thank you for prodding me into fixing this long-standing annoyance!

step-

Good fix, thank you for fixing it!