Shells in OCaml
1Attributes on expansions and params.
2
3Using default values:
4
5 $ sh -c "echo \${FOOBAR-hello}"
6 hello
7 $ msh -c "echo \${FOOBAR-hello}"
8 hello
9
10Smallest prefix pattern:
11
12 $ cat > test.sh << EOF
13 > x="foobar"
14 > echo "\${x#foo}"
15 > echo "\${x#f*}"
16 > echo "\${x#bar}"
17 > EOF
18
19 $ sh test.sh
20 bar
21 oobar
22 foobar
23 $ msh test.sh
24 bar
25 oobar
26 foobar
27
28Largest Prefix Pattern:
29
30 $ cat > test.sh << EOF
31 > x=/path/to/my/script.sh
32 > echo "\${x##*/}"
33 > EOF
34
35 $ sh test.sh
36 script.sh
37 $ msh test.sh
38 script.sh
39
40Smallest suffix pattern:
41
42 $ cat > test.sh << EOF
43 > x="foobar"
44 > echo "\${x%bar}"
45 > echo "\${x%o*}"
46 > echo "\${x%foo}"
47 > EOF
48
49 $ sh test.sh
50 foo
51 fo
52 foobar
53 $ msh test.sh
54 foo
55 fo
56 foobar
57
58Largest suffix pattern:
59
60 $ cat > test.sh << EOF
61 > x=/path/to/my/script.sh
62 > echo "\${x%%*/}"
63 > EOF
64
65 $ sh test.sh
66 /path/to/my/script.sh
67 $ msh test.sh
68 /path/to/my/script.sh
69
70Length of param:
71
72 $ cat > test.sh <<EOF
73 > foo=hello
74 > echo \${#foo}
75 > EOF
76
77 $ sh test.sh
78 5
79 $ msh test.sh
80 5
81
82Assigning on the fly:
83
84 $ cat > test.sh << EOF
85 > echo \${FOO:=bar}
86 > echo \$FOO
87 > EOF
88
89 $ sh test.sh
90 bar
91 bar
92 $ msh test.sh
93 bar
94 bar