Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.bash_logout commands get called with preexec repeating the last command line if logging out with CTRL+D #27

Open
davefx opened this issue Jul 25, 2016 · 5 comments
Assignees

Comments

@davefx
Copy link

davefx commented Jul 25, 2016

When logging out from an Ubuntu ssh session with CTRL+D in an empty command line, the preexec function gets called with the previous "$1" while executing the line /usr/bin/clear_console -q from ~/.bash_logout.
For example:

$ ssh localhost
davefx@localhost's password: 
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-31-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

91 packages can be updated.
0 updates are security updates.

Last login: Mon Jul 25 07:59:53 2016 from 127.0.0.1
davefx@localhost:~ $ preexec() { echo «$1»; sleep 3; }
davefx@localhost:~ $ echo Hello
«echo Hello»
Hello
davefx@laetus:~ $ logout
«echo Hello»
Connection to localhost closed.
davefx@laetus:~ $ 

(The logout was generated pressing CTRL+D)

@rcaloras
Copy link
Owner

@davefx thanks for reporting! Giving it a look.

@rcaloras rcaloras self-assigned this Jul 26, 2016
@jombooth
Copy link
Contributor

jombooth commented Sep 11, 2017

I have an idea for fixing this; the output of fc -l (a replacement for history, see #53) gives a number for each command, like this:
1048 set +x
1049 ls
1050 ld
1051 fc -l 0

if we store the last processed command number, then we can skip preexec in the case it hasn't changed.

@dimo414
Copy link
Collaborator

dimo414 commented Sep 11, 2017

Would it make sense to replace the existing short-circuit checks in __bp_preexec_invoke_exec with this check? Put another way is it the case that preexec should fire if-and-only-if the command number has changed?

@rcaloras
Copy link
Owner

I've considered this before. @jombooth What about the case where we have duplicate commands and ignoredups set? e.g. You enter the same command twice. History will not change (no increment in number) but we should invoke preexec.

@JeffFaer
Copy link

JeffFaer commented Nov 4, 2018

What about comparing this_command to BASH_COMMAND in __bp_preexec_invoke_exec?

$ tmux
tmux $ foo() {
> if [[ -z $i ]]; then
> i=0
> else
> ((i++))
> fi
> (set -o posix; set) > ~/env.$i
> }
tmux $ preexec_functions+=(foo)
tmux $ echo 1 2 3
tmux $ <CTRL + D>
$ grep 'this_command' ~/env.0 ~/env.1
~/env.0:this_command='echo 1 2 3'
~/env.1:this_command='echo 1 2 3'
$ diff ~/env.0 ~/env.1
4,5c4,5
< BASH_ARGC=()
< BASH_ARGV=()
---
> BASH_ARGC=([0]="1")
> BASH_ARGV=([0]="/home/jeffrey/.bash_logout")
7c7
< BASH_COMMAND='echo 1 2 3'
---
> BASH_COMMAND='[ "$SHLVL" = 1 ]'
9,10c9,10
< BASH_LINENO=([0]="67" [1]="10")
< BASH_SOURCE=([0]="main" [1]="/home/jeffrey/src/bash-preexec/bash-preexec.sh")
---
> BASH_LINENO=([0]="213" [1]="5" [2]="11")
> BASH_SOURCE=([0]="main" [1]="/home/jeffrey/src/bash-preexec/bash-preexec.sh" [2]="/home/jeffrey/.bash_logout")
26c26
< FUNCNAME=([0]="foo" [1]="__bp_preexec_invoke_exec")
---
> FUNCNAME=([0]="foo" [1]="__bp_preexec_invoke_exec" [2]="source")
103c103
< __bp_last_argument_prev_command=
---
> __bp_last_argument_prev_command=3
723c723
< i=0
---
> i=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants