Just a few things I find helpful :)
Bash history with timestamp.
This is more of an audit thing, but sometimes it's useful to know when you did something.
If you type the command history into your shell. You get back a list of the last X number of command executed.
$ history | tail -n1 502 history | tail -n1
But by adding the following to your ~/.bashrc (local user) or /etc/bash/bashrc (all users) file, you can inject the time/date. Please see man date for the options you can use.
HISTTIMEFORMAT='%F %T '
Example
$ history | tail -n1 508 2017-01-14 11:29:58 history | tail -n1
Bash History Duplicate Removal
It's an annoyance when you execute a command consecutively many time and then have to search further back to get to the last different command. Behold! Add this to your .bashrc(local user) or /etc/bash/bashrc (all users) and no matter how many times you execute that command consecutively, it will only store the one time.
HISTCONTROL=ignoreboth
Example
user@server ~ $ vi .bashrc user@server ~ $ vi .bashrc user@server ~ $ vi .bashrc user@server ~ $ vi .bashrc user@server ~ $ history | tail -n5 508 2017-01-14 11:29:58 history | tail -n1 509 2017-01-14 11:31:36 man date 510 2017-01-14 11:32:43 history 511 2017-01-14 11:40:52 vi .bashrc 512 2017-01-14 11:42:36 history | tail -n5
Double tap exit/logoff
Accidentally logging out of a shell session or user session can be annoying. But there is hope :) Add this to your .bashrc(local user) or /etc/bash/bashrc (all users) and now you have to double tap to get out.
This one ideally needs to go into the /etc/bash/bashrc or else it would only work when closing your own shell session.
IGNOREEOF=1
Example
server ~ # Use "logout" to leave the shell. server ~ # logout user@server ~ $
Extract Path or File
$ export VAR=/home/me/mydir/file.c
$ echo "${VAR%/*}"
/home/me/mydir
$ echo "${VAR##*/}"
file.c
Cron Execution Only
Pop this at the top of a bash script.
Eg. The below is for a cronjob that is executed at reboot:
# crontab -l
@reboot /bin/bash /path/to/something/bad.sh
#!/usr/bin/env bash
if [[ ! $(< /proc/${PPID}/comm) == cron* ]]; then
echo "Do NOT manually execute this script. ONLY at reboot!!!!"
exit
fi