Question: I am using a combination of xautolock and slimlock to automatiacally lock my machine if it does not receive input.

~/.xinitrc:

```
xautolock -locker /usr/bin/lockify.sh -time 5 &

```

/usr/bin/lockify.sh:

```
#!/bin/bash
DONTLOCK=false

if pidof gnome-mplayer 
then
    DONTLOCK=true
else
    echo "gnome-mplayer is not running"
fi

if $DONTLOCK
then
    echo "Watching Video, not locking screen"
else
    /usr/bin/slimlock
fi

```

Only recently I discovered that this is a problem, when I have a VMWare guest running in the foreground. What happens is that when the lock-screen is engaged the keyboard input seems to remain with my vmware-guest so I cannot unlock my host system.

Does anyone know how I could ensure that input focus is transferred back to my host before the system is locked?

Answer: 
**For bash v4+ :**

Use interactive mode in shell.

Set she-bang as

```
#!/bin/bash -i

```

and `history` inside of your script will work.

```
 $ cat test.sh
#!/bin/bash
history | wc -l
 $ ./test.sh
0
 $ sed -i '1s/bash/bash -i/' test.sh
 $ ./test.sh
6495

```

Commands was executed inside of script are not recorded into history.

**For bash v3 (and possible for older ones)**

* The way above doesn't work for this vesions of bash. However you can remove she-bang at all and history will work well. This way also works great for bash v4.
* set she-bang as interactive and do not forget about `set -o history` like chepner mentioned.

```
#!/bin/bash -i
set -o history
```

---

PS. `history |tail -n2 |head -n1` doesn't equal to the last command. it is command before last one.

Note if last or prelast commands were multiline it won't return the correct result.

btw in console you can use `!-2` to refer to prelast command instead of your strange construction. unfortunately it seems doesn't work in shell script even in interactive mode.
Does this help?

```
lastCommand=$(`cat ~/.bash_history |tail -n2 |head -n1` | sed 's/[0-9]* //')

```

Or maybe this:

```
echo !!

```

As bash have a build in facilities to provide last command