Showing posts with label bashrc. Show all posts
Showing posts with label bashrc. Show all posts

Monday, October 26, 2009

Better Console Calculator Using bc

If we use expr for mathematical calculations on console (terminal), it is frustrating, and hard to remember syntax and escape sequences used in expr. :( This small tip will help us.

Just add following line in your .bashrc file (~/.bashrc).

function calc
{
echo "${1}" | bc -l;
}

Then update the shell environment by:

$ source ~/.bashrc


"calc" from the shell will work as follows:

$ calc 2+3
5
$ calc 1+2*3
7
$ calc 1.1*2
2.2
$ calc "(1+2)*3"
9
$ calc "(1+2)^3"
27
$ calc "s(.5)"
.47942553860420300027

Tuesday, June 2, 2009

Web Access through Proxy Server by Terminal Applications

In many companies/Universities, the web access is granted through Proxy Server (Usually SQUID; hence port 3128).
There are many terminal applications (run on command line interface), which access Internet/Web. For example:
wget (to download file), ftp, lynx/links (to access website), apt/yum (to download and install package). If we are behind
proxy, these applications do not work. The easy solution is to set some shell environment variables, explained below:

For accessing web(lynx/links) using a non-authenticated proxy:
$ export http_proxy="http://proxy.yourcompany.com:3128"

Verify that the setting took place
$ echo $http_proxy
http://proxy.yourcompany.com:3128

For accessing web(lynx/links) using a authenticated proxy:
$ export http_proxy="http://username:password@proxy.yourcompany.com:3128"

If you want the change to be permanent (there each time you open a terminal),
add the export line to .bashrc in your 'home' directory.
$ echo 'export http_proxy="http://proxy.yourcompany.com:3128"'″ >> ~/.bashrc

Secure HTTP (over SSL) access
$ export https_proxy="https://proxy.yourcompany.com:3128"

FTP access
$ export ftp_proxy="ftp://proxy.yourcompany.com:3128"

Monday, June 9, 2008

Shortcuts for Working in BASH (Bourne Again SHell)

Navigation
Left/right cursor key --- Move left/right in text
Ctrl+A --- Move to beginning of lIne
Ctrl+E --- Move to end of line
Ctrl+right arrow --- Move forward one word
Ctrl+left arrow --- Move left one word

Editing
Ctrl+U --- Delete everything behind cursor to start of line
Ctrl+K --- Delete from cursor to end of line
Ctrl+W --- Delete from cursor to beginning of word
Alt+D --- Delete from cursor to end of word
Ctrl+T --- Transpose characters on left and right of cursor
Alt+T --- Transpose words on left and right of cursor

Miscellaneous
Ctrl+L --- Clear screen (everything above current line)
Ctrl+U --- Undo everything since last command
Alt+R --- Undo changes made to the line
Ctrl+Y --- Undo deletion of word or line caused by using Ctrl+K, Ctrl+W, and so on
Alt+L --- Lowercase current word (from the cursor to end of word)


Note: If you find these shortcuts hard to remember and you know vi(m),
you can enable vi mode for editing command line using following command:

$ set -o vi

To enable vi mode from start of Bash, add following lines to your ~/.bashrc
# Start vi Mode for command line editing
set -o vi



Wednesday, May 14, 2008

Typing Error Correction at Command Line

Open your Bash Resource Config file

$ vim ~/.bashrc

...
alias vmi='vim'
alias mvi='vim'
...
$ mvi ~/.bashrc
will open .bashrc

Friday, April 4, 2008

Prevent Yourself from Disaster

Edit your .bashrc file
$ vi ~/.bashrc

# prevent overwriting to existing file, while using redirection
set -o noclobber

# useful aliases
alias mv='mv -i'
alias cp='cp -i'
alias rm='rm -i'