Friday, July 19, 2019

bashrc Tips and Tricks



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 lsps 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 the HISTFILE 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_historyfile.

shopt -s histappend
PROMPT_COMMAND="history -n; history -a"
unset HISTFILESIZE
HISTSIZE=2000
https://stackoverflow.com/questions/19454837/bash-histsize-vs-histfilesize
HISTSIZE 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.

https://sanctum.geek.nz/arabesque/better-bash-history/
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'
https://www.shellhacks.com/tune-command-line-history-bash/
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


https://www.lifewire.com/bashrc-file-410194
The .bashrc file is a shell script which is run every time a user opens a new shell.
https://medium.com/@tzhenghao/a-guide-to-building-a-great-bashrc-23c52e466b1c
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-interactively
When 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:
  1. source your .bashrc explicitly
    #!/bin/bash
    . ~/.bashrc
    lookup "foo"
  2. start bash with the interactive flag
    #!/bin/bash -i
    lookup "foo"
  3. 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"

  • 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.

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts