bash

33
bash g 11/2010 Akron Linux User Group

description

Presentation I delivered that covered bash shell at Akron Linux User Group on 11/04/2010

Transcript of bash

Page 1: bash

bash

g11/2010

Akron Linux User Group

Page 2: bash

macroprocessor

program that expands text and symbols to create large expressions

Page 3: bash

shell

macroprocessor to execute commands

Page 4: bash

shell standard

IEEE POSIX and Open Group shell specification

http://www.opengroup.org/onlinepubs/9699919799/nframe.html

All of these mean the same thing:

POSIX.1-2008IEEE Std 1003.1-2008 Open Group Technical Standard Base Specifications Issue 7"

Page 5: bash

bash

shell that interprets certain default commands and that provides programmatic tools to write custom commands that don't exist can be used interactively (user types commands) or non-interactively (commands saved in a file)

respects POSIX.1-2008 standard maintained by Chet Ramey at the moment

Page 6: bash

bash documentation

man bashman builtinsman rbashman bashbug

Page 7: bash

bash feature summary 1/2source:http://tiswww.case.edu/php/chet/bash/bash-intro.html

editing and completion history and command re-entry job controlfunctions and aliasesarraysarithmeticansi c quote expansiontilde expansionbrace expansionsubstring capabilities

Page 8: bash

bash feature summary 2/2source:http://tiswww.case.edu/php/chet/bash/bash-intro.html

indirect variable expansion i/o capabilitycontrol of built-in commandsshell optional behaviorprompt customizationsecuritydirectory stackposix modeinternationalizationcommand timing

Page 9: bash

bash breakdown of tasks

read text (terminal, file, string)find words and operatorsparse tokens into commandsexpand certain tokens into lists

filenamescommandsarguments

handle redirectionsexecute commands wait for completion, or not

Page 10: bash

bash: types of expansions

tilde~

variablestr=abcdefgh; echo ${str:-3:2}

commandecho `date`

processcmp < (echo "my life") <(echo "your life")

arithmeticstr=abcdefgh; echo ${str:((-3)):2}

braceecho sp{el,il,al}l

Page 11: bash

bash modes

default (non POSIX)POSIX

Page 12: bash

bash: POSIX mode

set -o posix #now in POSIX mode

Page 13: bash

bash: POSIX mode

example of variance:set builtin does not show you func defs

Page 14: bash

bash: POSIX mode

subset of bash core no process substitutionwhy use it?

Page 15: bash

readline

bash + readline = productivityreadline allows you to make edits to commandsmaintained by Chet as welltypically gets updated along with bash

Page 16: bash

readline documentation

man bashthen type /READLINE <enter>

Page 17: bash

readline: take home

C-b move back oneC-f move forward oneC-d delete character I'm onC-u undo last keystroke (infinitely) C-a go to begin of lineC-e go to end of lineM-f jump forward (word)M-b jump backward (word)C-l clear screen, but keep my lineplenty more..

Page 18: bash

bash tips

Execute your script, after marking it executable chmod +x myscript.sh; ./myscript.sh

Page 19: bash

bash tips

Expansions!echo a{z,y,x}b

Page 20: bash

bash tips

Appendecho "This is me" > saveforlaterecho "This is also me" >> saveforlater

Page 21: bash

bash tips

Redirect output and error to different files. myprogram 1>myoutput 2>myerrors

To the same file. myprogram >&alloutput. myprogram &>alloutput

Page 22: bash

bash tips

First or last few lineshead novel_that_i_started_but_didnt_finishtail novel_that_i_started_but_didnt_finishtail +1000 novel_that_i_started_but_didnt_finishtail +1000 --lines=2000 novel_that_i_started_but_didnt_finish

Page 23: bash

bash tips

First or last few lineshead novel_that_i_started_but_didnt_finishtail novel_that_i_started_but_didnt_finishtail +1000 novel_that_i_started_but_didnt_finishtail +1000 --lines=2000 novel_that_i_started_but_didnt_finish

Page 24: bash

bash tips

accidental overwritesecho "Test String" > 1.fileecho "Test String2" > 1.fileset -o noclobber echo "Test String3" > 1.fileecho "Test String3" >> 1.file cat 1.file

Page 25: bash

bash tips

accidental overwritesecho "Test String" > 1.fileecho "Test String2" > 1.fileset -o noclobber echo "Test String3" > 1.file #no goecho "Test String3" >> 1.file #still works cat 1.file

Page 26: bash

bash tips

incidental (conscious) overwritesecho "Test String" > 1.fileecho "Test String2" > 1.fileset -o noclobber echo "Test String3" >| 1.file #goesecho "Test String3" >> 1.file #still works cat 1.file

Page 27: bash

bash tips

modern loops:for i in $(seq 1 10); do echo $i; done

Page 28: bash

bash tips

loops from the past:for i in {1..10}; do echo $i; done

Page 29: bash

bash tips

loops that are the most portable:for ((i=0; i<10; i++)); do echo $i; done

Page 30: bash

bash tips

portable stdout:printf "%s" "short string"

Page 31: bash

bash tips

what does the machine see:hexdump -C myfavoritebigfile

Page 32: bash

YOUR bash tips

Page 33: bash

bash online

wiki:http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29stallman:http://www.gnu.org/software/bash/chet:http://tiswww.case.edu/php/chet/bash/bashtop.htmlaustin:http://www.unix.org/single_unix_specification_v3/