summaryrefslogtreecommitdiff
path: root/gemfeed/2023-12-10-bash-golf-part-3.gmi
blob: d7632b5f88dad5fa0bf64a12b22f89a1d16b8169 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Bash Golf Part 3

> Published at 2023-12-10T11:35:54+02:00

```

    '\       '\        '\                   .  .          |>18>>
      \        \         \              .         ' .     |
     O>>      O>>       O>>         .                 'o  |
      \       .\. ..    .\. ..   .                        |
      /\    .  /\     .  /\    . .                        |
     / /   .  / /  .'.  / /  .'    .                      |
jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                        Art by Joan Stark, mod. by Paul Buetow
```

This is the third blog post about my Bash Golf series. This series is random Bash tips, tricks, and weirdnesses I have encountered over time. 

=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1
=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2
=> ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 (You are currently reading this)

## `FUNCNAME`

`FUNCNAME` is an array you are looking for a way to dynamically determine the name of the current function (which could be considered the callee in the context of its own execution), you can use the special variable `FUNCNAME`. This is an array variable that contains the names of all shell functions currently in the execution call stack. The element `FUNCNAME[0]` holds the name of the currently executing function, `FUNCNAME[1]` the name of the function that called that, and so on.

This is particularly useful for logging when you want to include the callee function in the log output. E.g. look at this log helper:

```bash
#!/usr/bin/env bash

log () {
    local -r level="$1"; shift
    local -r message="$1"; shift
    local -i pid="$$"

    local -r callee=${FUNCNAME[1]}
    local -r stamp=$(date +%Y%m%d-%H%M%S)

    echo "$level|$stamp|$pid|$callee|$message" >&2
}

at_home_friday_evening () {
    log INFO 'One Peperoni Pizza, please'
}

at_home_friday_evening
```

The output is as follows:

```bash
❯ ./logexample.sh
INFO|20231210-082732|123002|at_home_friday_evening|One Peperoni Pizza, please
```

## `:(){ :|:& };:`

This one may be widely known already, but I am including it here as I found a cute image illustrating it. But to break `:(){ :|:& };:` down:

* `:(){ }` is really a declaration of the function `:`
* The `;` is ending the current statement
* The `:` at the end is calling the function `:`
* `:|:&` is the function body

Let's break down the function body `:|:&`: 

* The first `:` is calling the function recursively
* The `|:` is piping the output to the function `:` again (parallel recursion)
* The `&` lets it run in the background.

So, it's a fork bomb. If you run it, your computer will run out of resources eventually. (Modern Linux distributions could have reasonable limits configured for your login session, so it won't bring down your whole system anymore unless you run it as `root`!)

And here is the cute illustration:

=> ./2023-12-10-bash-golf-part-3/bash-fork-bomb.jpg Bash fork bomb

## Inner functions

Bash defines variables as it is interpreting the code. The same applies to function declarations. Let's consider this code:

```bash
#!/usr/bin/env bash

outer() {
  inner() {
    echo 'Intel inside!'
  }
  inner
}

inner
outer
inner
```

And let's execute it:

```
❯ ./inner.sh
/tmp/inner.sh: line 10: inner: command not found
Intel inside!
Intel inside!
```

What happened? The first time `inner` was called, it wasn't defined yet. That only happens after the `outer` run. Note that `inner` will still be globally defined. But functions can be declared multiple times (the last version wins):

```bash
#!/usr/bin/env bash

outer1() {
  inner() {
    echo 'Intel inside!'
  }
  inner
}

outer2() {
  inner() {
    echo 'Wintel inside!'
  }
  inner
}

outer1
inner
outer2
inner
```

And let's run it:

```
❯ ./inner2.sh
Intel inside!
Intel inside!
Wintel inside!
Wintel inside!
```

## Exporting functions

Have you ever wondered how to execute a shell function in parallel through `xargs`? The problem is that this won't work:

```bash
#!/usr/bin/env bash

some_expensive_operations() {
  echo "Doing expensive operations with '$1' from pid $$"
}

for i in {0..9}; do echo $i; done \
  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
```

We try here to run ten parallel processes; each of them should run the `some_expensive_operations` function with a different argument. The arguments are provided to `xargs` through `STDIN` one per line. When executed, we get this:

```
❯ ./xargs.sh
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
bash: line 1: some_expensive_operations: command not found
```

There's an easy solution for this. Just export the function! It will then be magically available in any sub-shell!

```bash
#!/usr/bin/env bash

some_expensive_operations() {
  echo "Doing expensive operations with '$1' from pid $$"
}
export -f some_expensive_operations

for i in {0..9}; do echo $i; done \
  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
```

When we run this now, we get:

```
❯ ./xargs.sh
Doing expensive operations with '0' from pid 132831
Doing expensive operations with '1' from pid 132832
Doing expensive operations with '2' from pid 132833
Doing expensive operations with '3' from pid 132834
Doing expensive operations with '4' from pid 132835
Doing expensive operations with '5' from pid 132836
Doing expensive operations with '6' from pid 132837
Doing expensive operations with '7' from pid 132838
Doing expensive operations with '8' from pid 132839
Doing expensive operations with '9' from pid 132840
```

If `some_expensive_function` would call another function, the other function must also be exported. Otherwise, there will be a runtime error again. E.g., this won't work:

```bash
#!/usr/bin/env bash

some_other_function() {
  echo "$1"
}

some_expensive_operations() {
  some_other_function "Doing expensive operations with '$1' from pid $$"
}
export -f some_expensive_operations

for i in {0..9}; do echo $i; done \
  | xargs -P10 -I{} bash -c 'some_expensive_operations "{}"'
```

... because `some_other_function` isn't exported! You will also need to add an `export -f some_other_function`!

## Dynamic variables with `local`

You may know that `local` is how to declare local variables in a function. Most don't know that those variables actually have dynamic scope. Let's consider the following example:

```bash
#!/usr/bin/env bash

foo() {
  local foo=bar # Declare local/dynamic variable
  bar
  echo "$foo"
}

bar() {
  echo "$foo"
  foo=baz
}

foo=foo # Declare global variable
foo # Call function foo
echo "$foo"
```

Let's pause a minute. What do you think the output would be?

Let's run it:

```
❯ ./dynamic.sh
bar
baz
foo
```

What happened? The variable `foo` (declared with `local`) is available in the function it was declared in and in all other functions down the call stack! We can even modify the value of `foo', and the change will be visible up the call stack. It's not a global variable; on the last line, `echo "$foo"` echoes the global variable content.


## `if` conditionals

Consider all variants here more or less equivalent:

```bash
#!/usr/bin/env bash

declare -r foo=foo
declare -r bar=bar

if [ "$foo" = foo ]; then
  if [ "$bar" = bar ]; then
    echo ok1
  fi
fi

if [ "$foo" = foo ] && [ "$bar" == bar ]; then
  echo ok2a
fi

[ "$foo" = foo ] && [ "$bar" == bar ] && echo ok2b

if [[ "$foo" = foo && "$bar" == bar ]]; then
  echo ok3a
fi

 [[ "$foo" = foo && "$bar" == bar ]] && echo ok3b

if test "$foo" = foo && test "$bar" = bar; then
  echo ok4a
fi

test "$foo" = foo && test "$bar" = bar && echo ok4b
```

The output we get is:

```
❯ ./if.sh
ok1
ok2a
ok2b
ok3a
ok3b
ok4a
ok4b
```

## Multi-line comments

You all know how to comment. Put a `#` in front of it. You could use multiple single-line comments or abuse heredocs and redirect it to the `:` no-op command to emulate multi-line comments. 

```bash
#!/usr/bin/env bash

# Single line comment

# These are two single line
# comments one after another

: <<COMMENT
This is another way a
multi line comment
could be written!
COMMENT
```

I will not demonstrate the execution of this script, as it won't print anything! It's obviously not the most pretty way of commenting on your code, but it could sometimes be handy!

## Don't change it while it's executed

Consider this script:

```bash
#!/usr/bin/env bash

echo foo
echo echo baz >> $0
echo bar
```

When it is run, it will do:

```
❯ ./if.sh
foo
bar
baz
❯ cat if.sh
#!/usr/bin/env bash

echo foo
echo echo baz >> $0
echo bar
echo baz
```

So what happened? The `echo baz` line was appended to the script while it was still executed! And the interpreter also picked it up! It tells us that Bash evaluates each line as it encounters it. This can lead to nasty side effects when editing the script while it is still being executed! You should always keep this in mind!


Other related posts are:

=> ./2021-05-16-personal-bash-coding-style-guide.gmi 2021-05-16 Personal Bash coding style guide
=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all
=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1
=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2
=> ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 (You are currently reading this)

E-Mail your comments to `paul@nospam.buetow.org` :-)

=> ../ Back to the main site