Bash

From AdminWiki

(Difference between revisions)
Jump to: navigation, search
(Key Combinations)
(Bash Programming)
Line 73: Line 73:
Bash is the most common shell form on linux, and most other unix systems.
Bash is the most common shell form on linux, and most other unix systems.
-
'''Just a small reminder. Before you start writing a long bash script, think if you can do this in perl.'''
+
'''Just a small reminder. Before you start writing a long bash script, think if you can do this in perl^W python.'''

Revision as of 13:05, 10 April 2009

Contents

Bash Handling

Job Control

Interactive

Key Combinations

I will not use the ^X notation of key combos since those are a bit misleading imo - after all, we are mostly using lower case letters, also people who modified the ^ or meta keys know how to substitute correctly. And of course the terminal type must be right.

ATTENTION: Bash's key combo mode can be set to editor-alike behaviour, specifically it can be set to behaving like emacs or to behaving like vi. You do this with:

set -o emacs 

for the emacs mode, obviously, and

set -o vi

for the vi mode.

Most distributions of the GNU bash come with emacs mode preset.

For the purpose of readability, we will give the key kombinations as follows:

  • Ctrl-a/A means: "Ctrl-a in emacs mode, "a" in vi mode"
  • Ctrl-r/" means: "Ctrl-r in emacs mode, Ctrl-r in vi mode"
  • Ctrl-o/- means: "Ctrl-o in emacs mode, nothing identical in vi mode (or not known to the author ;)"

A word about the vi mode

The vi mode is only "something like" the vi you know. Per default, you are in insert mode, you get to command mode (the mode where key combos will work) by pressing Esc. If you use Ctrl-c, you will be returned to a clear insert mode Prompt again. By pressing "i", you get back to the insert mode. Be aware that some vi key combos do not really behave alike in bash.

Searching in bash history

Of course you know that bash will pull lines from the history into the buffer with the up and down cursor keys.

Ctrl-r/" allows you to recall-by-typing from the bash history.

Executing in bash history

Ctrl-o/-(emacs) alone does nothing, but while pressed when on the line of a history entry (pulled with Ctrl - r or cursor keys), it will execute the bash history entry and when it exists, put the next command in history into the shell buffer.

Example:

philip@dinky:~$ vi test.c
philip@dinky:~$ make test
cc     test.c   -o test
philip@dinky:~$ ./test
Hello World!
philip@dinky:~$ (press Ctrl-r vi here)
(reverse-i-search)`vi': vi test.c (press enter here)
philip@dinky:~$ vi test.c
philip@dinky:~$ (press Ctrl-r mak here)
(reverse-i-search)`mak': make test (press Ctrl-o here)
philip@dinky:~$ make test
cc     test.c   -o test
philip@dinky:~$ ./test

Moving about the prompt/command line

There are some slight differences between the emacs and the vi versions, mostly about whats considered to be in the copy buffer, where the cursor offset will be etc.

  • ctrl-a/0 move the cursor to the beginning of the bash prompt/line
  • ctrl-e/A move the cursor to the end of line
  • ctrl-w/- cut the word previous to the cursor (including intermittent whitespace)
  • ctrl-u/" cut everything from line beginning to, but not including the cursor position
  • ctrl-k/", /D cut everything from, and including the cursor position to end of line
  • ctrl-_/u undo
  • alt-b/- move the cursor back one word
  • alt-f/- move the cursor forward one word
  • ctrl-y/p paste the last thing cut
  • -/h move cursor left
  • -/l move cursor right
  • -/a start inserting after current cursor position
  • -/D cut everything from, and not including
  • -/dd delete line (save it for pasting)
  • ctrl-c/" reset input buffer (do not save for pasting)
  • arrow-up/j move up through history
  • arrow-down/k move down through history

Bash Programming

Bash is the most common shell form on linux, and most other unix systems.

Just a small reminder. Before you start writing a long bash script, think if you can do this in perl^W python.


Error and Output Redirect

to redirect output and error

 ls -l 1>normal_out 2>error_out

if you want to have both in one file

 ls -l 1>normal_out 2>&1

Questions and Solutions

Below some examples for bash problems that might come up

Arrays in Bash:

 foo[0]=1;
 foo[1]=2;
 foo[2]=3;
 # loop
 for (( i=0; i<${#foo[@]}; i++ ));
 do
     echo ${foo[$i]};
 done;
 # more like a foreach
 for i in ${foo[@]};
 do
     echo $i;
 done;

Variable Variables

 foobar=5;
 bar="foobar";
 echo $foobar;
 echo $bar;
 echo ${!bar};

Variable Variables in Arrays

 foo[0]=1;
 foo[1]=2;
 foo[2]=3;
 foo_tcp[0]="AT";
 foo_upd[0]="AU";
 foo_tcp[1]="BT";
 foo_upd[1]="BU";
 foo_tcp[2]="CT";
 foo_upd[2]="CU";
 # loop
 for (( i=0; i<${#foo[@]}; i++ ));
 do
     echo ${foo[$i]};
     for k in upd tcp;
     do
         data=foo_${k}[$i];
         echo ${!data};
     done;
 done;

More Documentation

The best advanced guide for bash scripting: http://www.tldp.org/LDP/abs/html/

Personal tools