Bash

From Liki

Jump to: navigation, search

Bash is a type of terminal. Most users don't know the difference. However, you can change up your bash terminal in helpful, sometimes fun ways. The configuration files are kept in ~/.bashrc, where ~ means /home/user. For example,

[sysadmin@bach ~user]$ more .bashrc
PS1="\[\033[1;31m\]\u:\w>>\[\033[0m\]"
alias rm='rm -i'
alias ls='ls --color'
PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/home/user/bin:.
export PATH
export CVS_RSH=ssh
 
PGPLOT_DIR="/usr/local/pgplot/"
export PGPLOT_DIR
PGPLOT_DEV=/XWIN
export PGPLOT_DEV
 
alias n='ssh newton'

Here we will discuss the various elements in the above sample file

Contents

[edit] Prompt

Here

PS1="\[\033[1;31m\]\u:\w>>\[\033[0m\]"

changes the prompt, turning it into different colors and different styles. Colors are changed by the commands \[\033[#;#;#m\], where the first # sets the background color, the second set special attributes of the foreground color like bold, blinking, etc..., and the last # sets the foreground color. The foreground colors are

Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37

unless you're in an xterm where the "1" means bold of the original color. To obtain background colors add 10. Just putting "0" as the color returns it to the default (if it was not put in the prompt code above all typed text would be the same color as the prompt!).

Next, there escape commands that add certain elements to your prompt. Here, \u gives the current user name. Some other useful ones are

\d     the date  in  "Weekday  Month  Date"  format (e.g., "Tue May 26")
\h     the hostname up to the first `.'
\H     the full hostname
\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
\u     the username of the current user
\w     the basename  of the current working direc­tory
\W     the current working directory
\\     a backslash

Some elaborate examples can be found here.

[edit] Alias

Aliasing can be used to make one command stand for another. This can be useful to make certain options of a command become defaulted, or to correct common typose. For example, to make 'rm' ask for confirmation before deleting a file you can put

alias rm='rm -i'

You can also have 'ls' run with colored entries by

alias ls='ls --color'

and if often forget the space between 'cd' and '..' you can put

alias cd..='cd ..'

just make sure there is no space around the = sign in the assignment, or bash will parse the line into seperate commands and give an error.

[edit] Path

Your PATH can be modified with,

PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/home/user/bin:.
export PATH
export CVS_RSH=ssh

The path is searched from the left to the right. So if you have two programs of the same name in /bin and /home/usr/bin, the version in /bin will be run.

To run a binary not in the global path (in a local directory), type:

./example.out


[edit] Extras

Extras may be regarded with,

PGPLOT_DIR="/usr/local/pgplot/"
export PGPLOT_DIR
PGPLOT_DEV=/XWIN
export PGPLOT_DEV

SSH can be made easier with an alias

alias n='ssh newton'

for those lazy ones among us.

[edit] Glitches

It can happen that your .bashrc file can get forgotten, or maybe messed up. Recently, all the nifty little changes and personalizations I had made to mine got lost, and I wasn't able to edit it for some reason, even though the permissions were set properly. I fixed this in the terminal by typing

ls -l .bashrc*

and I got

-rw-r--r-- 1 caden physics 223 2006-09-19 10:17 .bashrc
-rw-r----- 1 caden physics 187 2006-07-05 17:51 .bashrc~
-rw-r----- 1 caden physics 124 2006-01-26 02:02 .bashrc.~1~
-rw-r----- 1 caden physics 127 2006-07-05 17:51 .bashrc.~2~
-rw-r----- 1 caden physics 164 2006-07-05 18:01 .bashrc.~3~
-rw-r----- 1 caden physics 164 2006-07-05 18:16 .bashrc.~4~
-rw-r----- 1 caden physics 163 2006-07-05 18:17 .bashrc.~5~
-rw-r----- 1 caden physics 185 2006-07-05 18:20 .bashrc.~6~
-rw-r----- 1 caden physics 223 2006-07-05 18:22 .bashrc.~7~
-rw-r----- 1 caden physics 246 2006-07-12 11:54 .bashrc.~8~

I used the cat command to find the ~file that had all my changes (I started looking at the most recent one), and copied that file into .bashrc

cp .bashrc.~8~ .bashrc

Closed and reopened the terminal, and the problem was fixed!