introduction shell interface available on any UNIX-like OS interesting: both UI and API shell input [XCU 2.1] specified file '-c' option argument stdin (interactive mode) first steps: `echo` command [XCU 4] ex. 1: Hello world demonstration of input methods ex. 1.1: [sh] $ echo Hello world ex. 1.2: [hello.sh] echo Hello world [sh] $ sh hello.sh ex. 1.3: [sh] $ sh -c 'echo Hello world' magic number behavior, `chmod` [XCU 4], file modes ex. 2: magic hello [hello-magic.sh] #! /bin/sh echo Hello world [sh] $ chmod a+x $ sh hello-magic.sh field splitting [XCU 2.6.5] simple commands: `cd`, `ls`, `mkdir`, `touch` [XCU 4] ex. 3: field splitting [sh] $ mkdir foo bar $ ls tilde expansion [XCU 2.6.1] ex. 4: tilde expansion [sh] $ ls ~ $ ls ~sh-foo $ ls ~sh-foo/some-directory pathname expansion [XCU 2.6.6] [XCU 2.13] ex. 5: pathname expansion [sh] $ echo ~sh-foo/*-file $ echo ~sh-foo/ba*/file parameters: assignment ex. 6: parameter assignment [sh] $ myparam=foo $ mynum=1 parameters: expansion [XCU 2.6.2] simple parameter use default values string length remove {smallest,largest} suffix pattern remove {smallest,largest} prefix pattern ex. 7: parameter expansion [sh] $ echo ${myparam} $ echo ${nullparam:-default} $ echo ${#myparam} $ file=main.c $ echo ${file%.c}.o $ dir=foo/bar/baz $ echo ${dir##*/} positional parameters [XCU 2.5.1] ex. 8: positional parameters [pos-params.sh] #! /bin/sh echo ${1} echo ${2} [sh] $ chmod a+x pos-params.sh $ sh pos-params.sh foo $ ./pos-params.sh foo bar baz special parameters [XCU 2.5.2] @, *, #, ?, 0 exit status [XCU 2.8.2] `false` and `true` commands [XCU 4] ex. 9: special parameters [spec-params.sh] #! /bin/sh echo Number of positional parameters: ${#} echo Positional parameters: ${@} echo Script name: ${0} true echo ${?} false echo ${?} [sh] $ chmod a+x spec-params.sh ./spec-params.sh foo bar baz shell variables [XCU 2.5.3] HOME, IFS, PATH, PS1, PWD ex. 10: shell variables [sh] $ echo ${HOME} $ echo ${PWD} [sh (another shell)] $ PS1='$ ' $ PS1='PROMPT> ' command substitution [XCU 2.6.3] two versions ex. 11: command substitution [sh] $ filepath=$(basename ~sh-foo/bar/file) $ echo ${filepath} arithmetic expansion [XCU 2.6.4] ex. 12: arithmetic expansion $ echo $((2+3)) $ echo $((0x10 + 4)) $ i=0 $ i=$(($x + 1)) $ echo ${i} basic foundation complete simple commands revisited [XCU 2.9.1] simple-command: [parameter-assignment...] [word...] command search and execution pipelines [XCU 2.9.2] lists [XCU 2.9.3] AND-OR list list ex. 13: lists [sh] $ false && echo foo || echo bar $ true || echo foo && echo bar compound commands [XCU 2.9.4] grouping if, `test` command while until for case functions [XCU 2.9.5] I/O redirection [XCU 2.7]