https://sanctum.geek.nz/arabesque/better-bash-history/
https://gist.github.com/zachbrowne/8bc414c9f30192067831fafebd14255c
PROMPT_COMMAND
If set, the value is interpreted as a command to execute before
the printing of each primary prompt ($PS1).
PROMPT_COMMAND can contain ordinary bash statements whereas the PS1 variable can also contain the special characters, such as '\h' for hostname, in the variable.
https://www.shellhacks.com/tune-command-line-history-bash/
6. Ignore Specific Commands
HISTIGNORE
is a colon-separated list of patterns used to decide which command lines should be saved in the history file.
Don’t save
ls
, ps
and history
commands:export HISTIGNORE="ls:ps:history"
Don’t save commands with
s
in the beginig:export HISTIGNORE="s*"https://stackoverflow.com/questions/15116806/how-can-i-see-all-of-the-bash-history
You should look into the
histappend
shell option and the -a
flag to history
:histappend
If set, the history list is appended to the file named by the value of theHISTFILE
variable when the shell exits, rather than overwriting the file.history
-a
Append the "new" history lines (history lines entered since the beginning of the current bash session) to the history file.
If you put
history -a
into your PROMPT_COMMAND
, you'll get an always-up-to-date .bash_history
file.shopt -s histappend
PROMPT_COMMAND="history -n; history -a"
unset HISTFILESIZE
HISTSIZE=2000
https://stackoverflow.com/questions/19454837/bash-histsize-vs-histfilesizeHISTSIZE
is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.HISTFILESIZE
is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of your bash session for use in future sessions.HISTFILESIZE=1000000
HISTSIZE=1000000
You can prevent commands that start with a space from going into history by setting
$HISTCONTROL
to ignorespace
. You can also ignore duplicate commands, for example repeated du
calls to watch a file grow, by adding ignoredups
. There’s a shorthand to set both in ignoreboth
.HISTCONTROL=ignoreboth
You might also want to remove the use of certain commands from your history, whether for privacy or readability reasons. This can be done with the
$HISTIGNORE
variable. It’s common to use this to exclude ls
calls, job control builtins like bg
and fg
, and calls to history
itself:HISTIGNORE='ls:bg:fg:history'
Store history immediately
By default, Bash only records a session to the .bash_history
file on disk when the session terminates. This means that if you crash or your session terminates improperly, you lose the history up to that point. You can fix this by recording each line of history as you issue it, through the $PROMPT_COMMAND
variable:
PROMPT_COMMAND='history -a'
export HISTTIMEFORMAT="%h %d %H:%M:%S "
A note about privileged access
You can add code as follows in ~/.bashrc:
# if user is not root, pass all commands via sudo #
if [ $UID -ne 0 ]; then
alias reboot='sudo reboot'
alias update='sudo apt-get upgrade'
fi
The .bashrc file is a shell script which is run every time a user opens a new shell.
If you’re using a Mac, chances are that you will type these in your
~/.bash_profile
as opposed to your ~/.bashrc
.
There is a simple workaround for using
.bashrc
on MacOS: adding this snippet in .bash_profile
and making your configs in a new .bashrc
file.# Put this in your .bash_profile file.
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
I dislike typing exit, so I did qand it closes the terminal.
I dislike typing the word clear, so I hit cand it’s done.
alias q=’exit’
alias c=’clear’
Directories
alias home='cd ~'
alias root='cd /
alias dtop='cd ~/Desktop'
alias dbox='cd ~/Dropbox'
alias box='cd ~/Box\ Sync'
alias gdrive='cd ~/Google\ Drive'
alias ..='cd ..'
alias ...='cd ..; cd ..'
alias ....='cd ..; cd ..; cd ..'
NOTE: on Ubuntu, do this instead of open:
alias o=xdg-open
Program Aliases
SSH
Missing that Linux box on the other side of the world?
alias myec2box=’ssh <insert whatever here>'
alias pythonserver=’python -m SimpleHTTPServer 8000'
UNIX Command Options
# Let there be color in grep!
export GREP_OPTIONS=' — color=auto'
# Set Vim as my default editor
export EDITOR=vim
.bash_profile
is executed for login shells, while .bashrc
is executed for interactive non-login shells.
When you login (type username and password) via console, either sitting at the machine, or remotely via ssh:
.bash_profile
is executed to configure your shell before the initial command prompt.
But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then
.bashrc
is executed before the window command prompt. .bashrc
is also run when you start a new bash instance by typing /bin/bash
in a terminal.Why two different files?
Say, you’d like to print some lengthy diagnostic information about your machine each time you login (load average, memory usage, current users, etc). You only want to see it on login, so you only want to place this in your
.bash_profile
. If you put it in your .bashrc
, you’d see it every time you open a new terminal window.
An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling
.bash_profile
instead of .bashrc
. Other GUI terminal emulators may do the same, but most tend not to.Recommendation
Most of the time you don’t want to maintain two separate config files for login and non-login shells — when you set a
PATH
, you want it to apply to both. You can fix this by sourcing .bashrc
from your .bash_profile
file, then putting PATH
and common settings in .bashrc
.To do this, add the following lines to
.bash_profile
:if [ -f ~/.bashrc ]; then source ~/.bashrc fi
https://serverfault.com/questions/3743/what-useful-things-can-one-add-to-ones-bashrc
A little tip for Bash if you are a sysadmin and work with root privileges a lot:
shopt -o noclobber
This will prevent you from accidentally destroying the content of an already existing file if you redirect output (>filename). You can always force overwriting with >|filena
alias rm='rm -iv'
Now, every time you use the command rm, bash will in reality execute rm -iv which means that you will be asked to confirm any removals
# Directory navigation aliases
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
export COLORFGBG='default;default'
# Locale and editor
export EDITOR=nano
export BROWSER="firefox '%s' &"
https://serverfault.com/questions/736186/cant-use-functions-exported-from-bashrc-in-a-bash-script
Invoked non-interactivelyWhen Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.
Specifically, none of the ~/.bashrc, ~/.profile, ~/.bash_profile are NOT sourced. ~/.bashrc is only invoked if the shell is an interactive shell.
You have a couple of options:
- source your .bashrc explicitly
#!/bin/bash . ~/.bashrc lookup "foo"
- start bash with the interactive flag
#!/bin/bash -i lookup "foo"
- set the BASH_ENV variable when you start your script:
BASH_ENV=$HOME/.bashrc /path/to/my/script
The difference between the environment variables VISUAL and EDITOR is that EDITOR should be able to operate without the use of “advanced” terminal capabilities, while VISUAL may be a full screen editor such as vi or emacs. If you call an editor via bash, it will first try to use the editor defined in VISUAL, and in case of failure (maybe because terminal does not support a full-screen editor) it will use the editor defined in
the variable EDITOR. The advice is to set EDITOR to the value “vi -e”
https://github.com/atom/atom/issues/59
export EDITOR="atom --wait"
the variable EDITOR. The advice is to set EDITOR to the value “vi -e”
https://github.com/atom/atom/issues/59
export EDITOR="atom --wait"
- PATH = Colon separated list of directories to search for commands.
- HOME = Current user's home directory.
- LOGNAME = Current user's name.
- SHELL = The user's preferred shell.
- EDITOR = The user's preferred text editor.
- MAIL = The user's electronic mail inbox location.