1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
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]
|