Home > bash > Tune your bash > .bash_profile | .bashrc | ./etc/profile

Tune your bash > .bash_profile | .bashrc | ./etc/profile

This is very useful things in my Linux = .bashrc and my Mac = .bash_profile

Linux = .bashrc | /etc/profile

I just found this useful link with .bashrc examples bashrc examples
I use “HISTTIMEFORMAT=’%F %T ‘ ”
Here is full list for timestamps TIMESTAMPS

Here is my HISTORY section in .bashrc

# don't put duplicate lines in the history. See bash(1) for more options
# don't overwrite GNU Midnight Commander's setting of `ignorespace'.
export HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups
# ... or force ignoredups and ignorespace
export HISTCONTROL=ignoreboth
 
# append to the history file, don't overwrite it
shopt -s histappend
 
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
 
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

/etc/profile

export PATH
export HISTTIMEFORMAT='%F %T '
HISTSIZE=100000        # history size use big motherfucker size
HISTFILESIZE=1000      # file log size
HISTCONTROL=erasedups # dont dublucate 
HISTCONTROL=ignorespace #ignorepsaces
DATA=`date`                   # variable for `date`
 
HISTFILE=~/.bash_history.$DATA  # histfile will look .bash_histori + command `date`

Here is something very useful for me and i put it in my .bashrc
1. random pass generator
2. simple console calculator
3. colours
4. easy access servers with alias

genpasswd() {
        local l=$1
        [ "$l" == "" ] && l=20 
        tr -dc A-Za-z0-9-!@%^*_ < /dev/urandom | head -c ${l} | xargs
}
function calc () {
        { echo "$*" | bc -l; }
}
 
 
alias ls='ls --color'
# i want my grep to color my search word and exclude my grep command
alias grep='grep -v grep | grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

Lets see it in action :

[14:21]vhristev@hristev:~$ genpasswd
nHwl*W8Yp4v__T-whmn2
[14:21]vhristev@hristev:~$ calc 54-4
50
[14:21]vhristev@hristev:~$ ps aux | grep nginx
root      1920  0.0  0.2  27772  1084 ?        Ss   Mar10   0:00 nginx: master process /usr/sbin/nginx
www-data  1921  0.0  0.4  28700  2416 ?        S    Mar10   0:01 nginx: worker process
www-data  1922  0.0  0.4  28536  2280 ?        S    Mar10   0:01 nginx: worker process
www-data  1925  0.0  0.4  28536  2244 ?        S    Mar10   0:02 nginx: worker process
www-data  1926  0.0  0.4  28536  2252 ?        S    Mar10   0:02 nginx: worker process
[14:22]vhristev@hristev:~$

Why I need put colors in my prompt ?
– We are humans and make mistakes.I remember one day how I execute command on different server and … in fact it was not so big deal but if it was ???
– If you have 1-2 machines its not so useful but if you have 10 or more you may want to put some RED or YELLOW color in your prompt to identify your important servers.

\A – the current time in 24-hour HH:MM format
33[00;31m – RED
33[01;32m – Yellow

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[33[00;31m\]\u@\[33[01;32m\]\h\[33[00m\]:\[33[01;34m\]\w\[33[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
 
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[33[0;35m\]\[33[01;33m\][\A]\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

And voalaaa… you get the prettiest bash prompt :
You can see another idea for customize your PS1 prompt

I want to access my servers very easy without typing every time USER/HOSTNAME/PORT in this case i use aliases
syntax= “alias NAME_OF_COMMAND=’real command’
example= alias homepc=’ssh -l root 192.168.10.10 -p 3333′

Now when i put this in my ~/.bashrc and update it (source ~/.bashrc) when i type “homepc” command “ssh -l root 192.168.10.10 -p 3333” will be executed
In action:

Bash special characters codes

    * \a : an ASCII bell character (07)
 
    * \d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
 
    * \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
 
    * \e : an ASCII escape character (033)
 
    * \h : the hostname up to the first '.'
 
    * \H : the hostname
 
    * \j : the number of jobs currently managed by the shell
 
    * \l : the basename of the shell’s terminal device name
 
    * \n : newline
 
    * \r : carriage return
 
    * \s : the name of the shell, the basename of $0 (the portion following the final slash)
 
    * \t : the current time in 24-hour HH:MM:SS format
 
    * \T : the current time in 12-hour HH:MM:SS format
 
    * \@ : the current time in 12-hour am/pm format
 
    * \A : the current time in 24-hour HH:MM format
 
    * \u : the username of the current user
 
    * \v : the version of bash (e.g., 2.00)
 
    * \V : the release of bash, version + patch level (e.g., 2.00.0)
 
    * \w : the current working directory, with $HOME abbreviated with a tilde
 
    * \W : the basename of the current working directory, with $HOME abbreviated with a tilde
 
    * \! : the history number of this command
 
    * \# : the command number of this command
 
    * \$ : if the effective UID is 0, a #, otherwise a $
 
    * \nnn : the character corresponding to the octal number nnn
 
    * \\ : a backslash
 
    * \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
 
    * \] : end a sequence of non-printing characters

Here is my MacBook .bash_profile some good function ps and grep some process example = psgrep apache will ps aux | grep apache .

Categories: bash Tags:
Автор
Вижте всички статии от
Този автор е написал 13 статии