Beyond Bash

Eduardo Bellido Bellido

http://edubxb.net

@edu2b

by

About bash

  • Free software
  • Part of the GNU Project
  • Default shell in GNU/Linux and Mac
  • Available in other OS (Windows included)
  • Shell Shock

Why bash?

  • Plenty of features
  • Always available, no need to learn new things
  • GNU readline powered
  • Extensible like other popular shells (zsh, fish)

GNU Readline library

  • Provides the advanced editing options, history, and other capabilities to Bash
  • Used by many projects, not only Bash
  • Very configurable

Time to improve!

with bash!

Let's go...

Configuration

Config files

When bash starts as login shell:

 

  ~/.bash_profile

 

When bash starts interactively:

 

  ~/.bashrc

 

GNU Readline config file:

 

  ~/.inputrc

Simplify things

Put all your configuration in .bashrc

Then, in .bash_profile, put:

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

Useful options

autocd → enter directories without typing cd


cdspell → correct minor spelling errors in directory names

 

extglob → enable extended pattern matching features


globstar → enable the pattern '**'


histappend → history list is appended rather than overwrited

Know your history

Don't repeat yourself

View your history

$ history
1 cd /tmp
2 wget https://www.kernel.org/pub/linux/kern
  el/v4.x/linux-4.0.4.tar.xz
3 tar xvJf linux-4.0.4.tar.xz
4 cd linux-4.0.4
5 make oldconfig
6 make -j 4
7 make modules_install
8 make install
# append history lines from current session to the history file
$ history -a

# read the history file and append the contents to the history list
$ history -r

Repeat your history

!n → Refer to command line n

!-n → Refer to the current command minus n

!! → Refer to the previous command (tip: sudo !!)

!string → Refer to the most recent command preceding the current position in the history list starting with string

!?string[?] → Refer to the most recent command preceding the current position in the history list containing string

^string1^string2^ → Repeat the previous command, replacing string1 with  string2

Navigating the history

C-p → Move 'back' through the history list, fetching the previous command

C-n → Move 'forward' through the history list, fetching the next command

M-< → Move to the first line in the history

M-> → Move to the end of the input history, i.e., the line currently being entered

C-r → Search backward starting at the current line and moving 'up' through the history as necessary

C-s → Search forward starting at the current line and moving 'down' through the the history as necessary (try "stty -ixon" if doesn't work)

C-g → Escape from history searching mode

A better history

HISTCONTROL='ignorespace:ignoredups:erasedups'
HISTSIZE=1000
HISTFILESIZE=3000
HISTTIMEFORMAT='[%F %T]  '

Some variables to control the history behavior:

Meet the "magic-space"

Expands the history commands

 

Enable it:

# Add those lines to your .inputrc file
$if Bash
  Space: magic-space
$endif

Bonus:

 

C-M-e → expand the line as the shell does

Movement & Edition

be faster!

Basic movements

C-a → Move to the start of the current line
C-e → Move to the end of the line
C-f → Move forward a character
C-b → Move back a character
M-f → Move forward to the end of the next word
M-b → Move back to the start of the current or previous word

Basic edition

M-Del → Delete the Word before the cursor
M-d → Delete the Word after the cursor
C-d → Delete character under the cursor
C-h → Delete character before the cursor (Backspace)

C-w → Cut the Word before the cursor to the clipboard
C-k → Cut the Line after the cursor to the clipboard
C-u → Cut/delete the Line before the cursor to the clipboard

C-y → Paste the last thing to be cut (yank)
C-_ → Undo

Patterns & Expansions

Extended patterns

?(pattern-list)

    Matches zero or one occurrence of the given patterns

*(pattern-list)

    Matches zero or more occurrences of the given patterns

+(pattern-list)

    Matches one or more occurrences of the given patterns

@(pattern-list)

    Matches one of the given patterns

!(pattern-list)

    Matches anything except one of the given patterns

Useful expansions I

${parameter:=word}

    Assign Default Values

 

${parameter:offset}

${parameter:offset:length}

    Substring  Expansion

Useful expansions II

${parameter#word}

${parameter##word}

    Remove matching prefix pattern

 

${parameter%word}

${parameter%%word}

    Remove matching suffix pattern

 

${parameter/pattern/string}

    Pattern substitution

Useful expansions III

${parameter^pattern}

${parameter^^pattern}

${parameter,pattern}

${parameter,,pattern}

    Case  modification

Extending bash

Cool projects to try

Powerline

Prompt improvements, written in python, a bit slow. Also supports more applications, Vim, Tmux...

Bash it

Collections of scripts, aliases, and completions for Bash, also provides some useful improvements for the prompt.

Rainbow bash!

Prompt improvements, supports plugins, currently only have a few.

Oh my git!

Improved git prompt, very very verbose.

Questions?

Thanks!

$ sudo applause

Beyond Bash

By Eduardo Bellido Bellido

Beyond Bash

  • 2,136