@renault-digital/bash-base 2.4.1 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/array.sh CHANGED
@@ -12,25 +12,28 @@
12
12
  # array_join '|' myArry ==> " a | b c "
13
13
  # @SEE_ALSO
14
14
  # string_split_to_array, array_describe, array_from_describe
15
- function array_join() {
15
+ array_join() {
16
16
  local delimiter="$1"
17
- local array="$2[@]"
17
+ local array="$2[@]" # Same local names as the pre-PR Bash implementation.
18
+ local element result="" delimiterLength
18
19
 
19
- local element result delimiterLength
20
- for element in "${!array}"; do
20
+ # Expand the named array directly into function positional parameters. This
21
+ # avoids introducing new local variables that shadow caller array names.
22
+ eval 'set -- "${'"$2"'[@]}"'
23
+ for element in "$@"; do
21
24
  result="${result}${element}${delimiter}"
22
25
  done
23
26
 
24
27
  delimiterLength=$(string_length "${delimiter}")
25
28
  if string_is_empty "${result}"; then
26
- echo ''
29
+ printf '\n'
27
30
  else
28
31
  string_sub 0 $((0 - delimiterLength)) "${result}"
29
32
  fi
30
33
  }
31
34
 
32
35
  # @NAME
33
- # array_describe -- convert the array to its string representation
36
+ # array_describe -- convert the array to its shell-native string representation
34
37
  # @SYNOPSIS
35
38
  # array_describe arrayVarName
36
39
  # @DESCRIPTION
@@ -40,8 +43,20 @@ function array_join() {
40
43
  # array_describe myArray ==> ([0]='a' [1]='b')
41
44
  # @SEE_ALSO
42
45
  # string_split_to_array, array_join, array_from_describe
43
- function array_describe() {
44
- declare -p "$1" | string_after_first "=" | tr '"' "'"
46
+ array_describe() {
47
+ local _bash_base_declaration _bash_base_values
48
+
49
+ _bash_base_declaration="$(typeset -p "$1")" || return 1
50
+ _bash_base_values="${_bash_base_declaration#*=}"
51
+
52
+ # macOS Bash 3.2 wraps array assignments in one outer single-quoted
53
+ # word. Newer Bash prints the compound assignment directly. Decode
54
+ # the outer shell quoting before preserving the existing output format.
55
+ if [[ -n "${BASH_VERSION:-}" && "$_bash_base_values" == \'*\' ]]; then
56
+ eval "_bash_base_values=$_bash_base_values" || return 1
57
+ fi
58
+
59
+ printf '%s\n' "$_bash_base_values" | tr '"' "'"
45
60
  }
46
61
 
47
62
  # @NAME
@@ -56,7 +71,7 @@ function array_describe() {
56
71
  # array_from_describe myNewArray < fileNameContentString
57
72
  # @SEE_ALSO
58
73
  # string_split_to_array, array_join, array_describe
59
- function array_from_describe() {
74
+ array_from_describe() {
60
75
  local newArrayVarName="$1"
61
76
  local string="${2-$(cat)}"
62
77
 
@@ -77,11 +92,14 @@ function array_from_describe() {
77
92
  # echo "ab" | array_contains arr
78
93
  # @SEE_ALSO
79
94
  # array_remove, array_in
80
- function array_contains() {
95
+ array_contains() {
81
96
  local array="$1[@]"
82
97
  local seeking="${2-$(cat)}"
83
98
 
84
- array_in "${seeking}" "${!array}"
99
+ # Use the same locals as before; adding a `values` or `arrayVarName`
100
+ # local here would hide callers' arrays with those legitimate names.
101
+ eval 'set -- "${'"$1"'[@]}"'
102
+ array_in "${seeking}" "$@"
85
103
  }
86
104
 
87
105
  # @NAME
@@ -96,7 +114,7 @@ function array_contains() {
96
114
  # array_in "a b" "a" "b" "c" "a b" "f" "g"
97
115
  # @SEE_ALSO
98
116
  # array_remove, array_contains
99
- function array_in() {
117
+ array_in() {
100
118
  local seeking="$1"
101
119
  shift
102
120
  local element
@@ -118,16 +136,14 @@ function array_in() {
118
136
  # array_sort myArray ==> ([0]='aa' [1]='aa' [2]='bb')
119
137
  # @SEE_ALSO
120
138
  # array_sort_distinct
121
- function array_sort() {
139
+ array_sort() {
122
140
  local arrayVarName="$1"
123
141
  local strSorted arrSorted
124
142
 
125
143
  strSorted="$(array_join $'\n' "${arrayVarName}" | sort)"
126
144
  string_split_to_array $'\n' arrSorted "${strSorted}"
127
145
 
128
- local string="\${arrSorted[@]}"
129
- local command="${arrayVarName}=(\"${string}\")"
130
- eval "${command}"
146
+ eval "${arrayVarName}=( \"\${arrSorted[@]}\" )"
131
147
  }
132
148
 
133
149
  # @NAME
@@ -141,16 +157,14 @@ function array_sort() {
141
157
  # array_sort_distinct myArray ==> ([0]='aa' [1]='bb')
142
158
  # @SEE_ALSO
143
159
  # array_sort
144
- function array_sort_distinct() {
160
+ array_sort_distinct() {
145
161
  local arrayVarName="$1"
146
162
  local strSorted arrSorted
147
163
 
148
164
  strSorted="$(array_join $'\n' "${arrayVarName}" | sort -u)"
149
165
  string_split_to_array $'\n' arrSorted "${strSorted}"
150
166
 
151
- local string="\${arrSorted[@]}"
152
- local command="${arrayVarName}=(\"${string}\")"
153
- eval "${command}"
167
+ eval "${arrayVarName}=( \"\${arrSorted[@]}\" )"
154
168
  }
155
169
 
156
170
  # @NAME
@@ -163,38 +177,30 @@ function array_sort_distinct() {
163
177
  # myArray=('aa' 'bb' 'aa')
164
178
  # array_length myArray ==> 3
165
179
  # @SEE_ALSO
166
- function array_length() {
180
+ array_length() {
167
181
  local arrayVarName="$1"
168
- local string command tmp
169
-
170
- eval "string='$'{${arrayVarName}[@]}"
171
- command="tmp=(\"${string}\")"
172
- eval "${command}"
182
+ local -a tmp
173
183
 
174
- echo "${#tmp[@]}"
184
+ eval "tmp=( \"\${${arrayVarName}[@]}\" )"
185
+ printf '%s\n' "${#tmp[@]}"
175
186
  }
176
187
 
177
188
  # @NAME
178
- # array_reset_index -- reset the indexes of array to the sequence 0,1,2..., save the result to original variable name
189
+ # array_reset_index -- reset indexes to the shell-native contiguous sequence, save the result to original variable name
179
190
  # @SYNOPSIS
180
191
  # array_reset_index arrayVarName
181
192
  # @DESCRIPTION
182
193
  # **arrayVarName** the variable name of the array to be processed
183
194
  # @EXAMPLES
184
- # myArray=([2]='a' [5]='c' [11]='dd')
195
+ # Bash example: myArray=([2]='a' [5]='c' [11]='dd')
185
196
  # array_reset_index myArray ==> ([0]='a' [1]='c' [2]='dd')
186
197
  # @SEE_ALSO
187
- function array_reset_index() {
198
+ array_reset_index() {
188
199
  local arrayVarName="$1"
189
- local string command tmp
190
-
191
- eval "string='$'{${arrayVarName}[@]}"
192
- command="tmp=(\"${string}\")"
193
- eval "${command}"
200
+ local -a tmp
194
201
 
195
- string="\${tmp[@]}"
196
- command="${arrayVarName}=(\"${string}\")"
197
- eval "${command}"
202
+ eval "tmp=( \"\${${arrayVarName}[@]}\" )"
203
+ eval "${arrayVarName}=( \"\${tmp[@]}\" )"
198
204
  }
199
205
 
200
206
  # @NAME
@@ -212,7 +218,7 @@ function array_reset_index() {
212
218
  # array_equals myArray1 myArray2 false && echo Y || echo N ==> N
213
219
  # array_equals myArray1 myArray2 true && echo Y || echo N ==> Y
214
220
  # @SEE_ALSO
215
- function array_equals() {
221
+ array_equals() {
216
222
  local arrayVarName1="$1"
217
223
  local arrayVarName2="$2"
218
224
  local ignoreOrder=${3:-true}
@@ -235,7 +241,7 @@ function array_equals() {
235
241
  array_reset_index tmp2
236
242
  fi
237
243
 
238
- [ "$(array_describe tmp1)" == "$(array_describe tmp2)" ]
244
+ [[ "$(array_describe tmp1)" == "$(array_describe tmp2)" ]]
239
245
  }
240
246
 
241
247
  # @NAME
@@ -254,25 +260,23 @@ function array_equals() {
254
260
  # array_intersection myArray1 myArray2 newArray false
255
261
  # @SEE_ALSO
256
262
  # array_subtract, array_union
257
- function array_intersection() {
263
+ array_intersection() {
258
264
  local array1="$1[@]"
259
265
  local arrayVarName2="$2"
260
266
  local newArrayVarName="$3"
261
267
  local ignoreOrderAndDuplicated=${4:-true}
262
-
263
268
  local tmp element2 string command
269
+
270
+ eval 'set -- "${'"$1"'[@]}"'
264
271
  tmp=()
265
- for element2 in "${!array1}"; do
272
+ for element2 in "$@"; do
266
273
  array_contains "$arrayVarName2" "$element2" && array_append tmp "$element2"
267
274
  done
268
275
 
269
276
  if [ "${ignoreOrderAndDuplicated}" = true ]; then
270
277
  array_sort_distinct tmp
271
278
  fi
272
-
273
- string="\${tmp[@]}"
274
- command="${newArrayVarName}=(\"${string}\")"
275
- eval "${command}"
279
+ eval "${newArrayVarName}=( \"\${tmp[@]}\" )"
276
280
  }
277
281
 
278
282
  # @NAME
@@ -291,25 +295,23 @@ function array_intersection() {
291
295
  # array_subtract myArray1 myArray2 newArray false
292
296
  # @SEE_ALSO
293
297
  # array_intersection, array_union
294
- function array_subtract() {
298
+ array_subtract() {
295
299
  local array1="$1[@]"
296
300
  local arrayVarName2="$2"
297
301
  local newArrayVarName="$3"
298
302
  local ignoreOrderAndDuplicated=${4:-true}
299
-
300
303
  local tmp element2 string command
304
+
305
+ eval 'set -- "${'"$1"'[@]}"'
301
306
  tmp=()
302
- for element2 in "${!array1}"; do
307
+ for element2 in "$@"; do
303
308
  array_contains "$arrayVarName2" "$element2" || array_append tmp "$element2"
304
309
  done
305
310
 
306
311
  if [ "${ignoreOrderAndDuplicated}" = true ]; then
307
312
  array_sort_distinct tmp
308
313
  fi
309
-
310
- string="\${tmp[@]}"
311
- command="${newArrayVarName}=(\"${string}\")"
312
- eval "${command}"
314
+ eval "${newArrayVarName}=( \"\${tmp[@]}\" )"
313
315
  }
314
316
 
315
317
  # @NAME
@@ -328,28 +330,25 @@ function array_subtract() {
328
330
  # array_union myArray1 myArray2 newArray false
329
331
  # @SEE_ALSO
330
332
  # array_intersection, array_union
331
- function array_union() {
333
+ array_union() {
332
334
  local array1="$1[@]"
333
335
  local array2="$2[@]"
334
336
  local newArrayVarName="$3"
335
337
  local ignoreOrderAndDuplicated=${4:-true}
336
-
337
338
  local tmp element2 string command
339
+
340
+ # Union consumes both arrays as a single ordered sequence. Both are
341
+ # expanded before positional arguments are replaced.
342
+ eval 'set -- "${'"$1"'[@]}" "${'"$2"'[@]}"'
338
343
  tmp=()
339
- for element2 in "${!array1}"; do
340
- array_append tmp "$element2"
341
- done
342
- for element2 in "${!array2}"; do
344
+ for element2 in "$@"; do
343
345
  array_append tmp "$element2"
344
346
  done
345
347
 
346
348
  if [ "${ignoreOrderAndDuplicated}" = true ]; then
347
349
  array_sort_distinct tmp
348
350
  fi
349
-
350
- string="\${tmp[@]}"
351
- command="${newArrayVarName}=(\"${string}\")"
352
- eval "${command}"
351
+ eval "${newArrayVarName}=( \"\${tmp[@]}\" )"
353
352
  }
354
353
 
355
354
  # @NAME
@@ -364,14 +363,15 @@ function array_union() {
364
363
  # array_append myArray "ele ment1" "ele ment2"
365
364
  # @SEE_ALSO
366
365
  # array_remove
367
- function array_append() {
366
+ array_append() {
368
367
  local arrayVarName="$1"
369
368
  shift
370
369
 
371
- local elementToAppend command
370
+ local elementToAppend
372
371
  for elementToAppend in "$@"; do
373
- command="$arrayVarName+=(\"${elementToAppend}\")"
374
- eval "${command}"
372
+ # Keep the value out of the eval program text; expand it only after eval
373
+ # has selected the destination array name.
374
+ eval "${arrayVarName}+=( \"\$elementToAppend\" )"
375
375
  done
376
376
  }
377
377
 
@@ -387,24 +387,19 @@ function array_append() {
387
387
  # array_remove arr "ab"
388
388
  # @SEE_ALSO
389
389
  # array_contains, array_append
390
- function array_remove() {
390
+ array_remove() {
391
391
  local arrayVarName="$1"
392
392
  local element="$2"
393
-
394
393
  local string command tmp index
395
- eval "string='$'{${arrayVarName}[@]}"
396
- command="tmp=(\"${string}\")"
397
- eval "${command}"
398
394
 
399
- for index in "${!tmp[@]}"; do
400
- if [[ "${tmp[$index]}" == "${element}" ]]; then
401
- unset tmp["${index}"]
395
+ eval 'set -- "${'"$arrayVarName"'[@]}"'
396
+ tmp=()
397
+ for index in "$@"; do
398
+ if [[ "$index" != "$element" ]]; then
399
+ tmp+=("$index")
402
400
  fi
403
401
  done
404
-
405
- string="\${tmp[@]}"
406
- command="${arrayVarName}=(\"${string}\")"
407
- eval "${command}"
402
+ eval "${arrayVarName}=( \"\${tmp[@]}\" )"
408
403
  }
409
404
 
410
405
  # @NAME
@@ -418,7 +413,7 @@ function array_remove() {
418
413
  # arr=(" a " " b c ")
419
414
  # array_clone arr newArray
420
415
  # @SEE_ALSO
421
- function array_clone() {
416
+ array_clone() {
422
417
  local arrayVarName="$1"
423
418
  local arrayVarName2="$2"
424
419
 
@@ -437,23 +432,27 @@ function array_clone() {
437
432
  # arr=(" a " " b c ")
438
433
  # array_map arr "string_trim | wc -m | string_trim" newArray
439
434
  # @SEE_ALSO
440
- function array_map() {
435
+ array_map() {
441
436
  local array="$1[@]"
442
437
  local pipedOperators="$2"
443
- local newArrayVarName="$3"
444
-
438
+ local newArrayVarName="${3-}"
445
439
  local tmp element mapped_value string command
440
+
441
+ eval 'set -- "${'"$1"'[@]}"'
446
442
  tmp=()
447
- for element in "${!array}"; do
448
- escaped="${element//\'/\'"\'"\'}" # escape the single quote
449
- mapped_value="$(eval "echo '${escaped}' | ${pipedOperators}")"
443
+ for element in "$@"; do
444
+ if [[ -n "${BASH_VERSION:-}" ]]; then
445
+ # Retain the original Bash echo semantics for values such as -n,
446
+ # but never evaluate an input element as shell source.
447
+ mapped_value="$(echo "$element" | eval "$pipedOperators")"
448
+ else
449
+ mapped_value="$(printf '%s\n' "$element" | eval "$pipedOperators")"
450
+ fi
450
451
  tmp+=("${mapped_value}")
451
452
  done
452
453
 
453
454
  if [[ -n "${newArrayVarName}" ]]; then
454
- string="\${tmp[@]}"
455
- command="${newArrayVarName}=(\"${string}\")"
456
- eval "${command}"
455
+ eval "${newArrayVarName}=( \"\${tmp[@]}\" )"
457
456
  else
458
457
  array_join $'\n' tmp
459
458
  fi
@@ -471,23 +470,22 @@ function array_map() {
471
470
  # arr=("NAME A" "NAME B" "OTHER")
472
471
  # array_filter arr 'NAME' newArray
473
472
  # @SEE_ALSO
474
- function array_filter() {
473
+ array_filter() {
475
474
  local array="$1[@]"
476
475
  local regExp="$2"
477
- local newArrayVarName="$3"
478
-
476
+ local newArrayVarName="${3-}"
479
477
  local tmp element string command
478
+
479
+ eval 'set -- "${'"$1"'[@]}"'
480
480
  tmp=()
481
- for element in "${!array}"; do
482
- if [[ ${element} =~ ${regExp} ]]; then
481
+ for element in "$@"; do
482
+ if [[ $element =~ $regExp ]]; then
483
483
  array_append tmp "${element}"
484
484
  fi
485
485
  done
486
486
 
487
487
  if [[ -n "${newArrayVarName}" ]]; then
488
- string="\${tmp[@]}"
489
- command="${newArrayVarName}=(\"${string}\")"
490
- eval "${command}"
488
+ eval "${newArrayVarName}=( \"\${tmp[@]}\" )"
491
489
  else
492
490
  array_join $'\n' tmp
493
491
  fi
package/src/constant.sh CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
- NEW_LINE_SED="\\$(echo -e '\r\n')" # Constant: the return and new line character, used with sed
3
+ NEW_LINE_SED="\\$(printf '\r\n')" # Constant: the return and new line character, used with sed
4
4
  export NEW_LINE_SED
5
5
 
6
6
  COLOR_BOLD_MAGENTA=$'\e[1;35m' # Constant: color for printing Header
package/src/document.sh CHANGED
@@ -18,9 +18,10 @@ source src/array.sh
18
18
  # doc_lint_script_comment "${shellScriptFile}"
19
19
  # @SEE_ALSO
20
20
  # doc_comment_to_markdown
21
- function doc_lint_script_comment() {
21
+ doc_lint_script_comment() {
22
22
  local shellScriptFile="$1"
23
- local element strAllFunctionsAndTheirTags arrAllFunctionsAndTheirTags manTags strFunctionAndItsTags arrFunctionAndItsTags intersection counter
23
+ local element strAllFunctionsAndTheirTags arrAllFunctionsAndTheirTags manTags strFunctionAndItsTags arrFunctionAndItsTags intersection
24
+ local counter=0 lastFunction=""
24
25
 
25
26
  # format the comment
26
27
  sed -E -i'.bk' \
@@ -28,22 +29,27 @@ function doc_lint_script_comment() {
28
29
  -e "s/^#/# /g" \
29
30
  -e "s/^#[[:space:]]+$/#/g" \
30
31
  -e "s/^#[[:space:]]*!/#!/g" \
31
- -e "s/^#[[:space:]]*(@|-|#+)/# \1/g" \
32
+ -e "s/^#[[:space:]]*(@|-|#+)/# \\1/g" \
32
33
  "${shellScriptFile}"
33
34
  rm -fr "${shellScriptFile}.bk"
34
35
 
35
- # valid comment tags by man page convention
36
- strAllFunctionsAndTheirTags=$(grep -e '^# @' -e '^function ' "${shellScriptFile}" | string_replace_regex '\(\)|#' '' | string_trim)
36
+ # Accept both historical `function name()` declarations and the shared
37
+ # Bash/zsh `name()` form.
38
+ strAllFunctionsAndTheirTags=$(grep -E -e '^# @' -e '^[[:space:]]*(function[[:space:]]+)?[0-9A-Za-z_-]+[[:space:]]*\(\)[[:space:]]*\{' "${shellScriptFile}" | string_replace_regex '\(\)|#' '' | string_trim)
37
39
  string_split_to_array "{" arrAllFunctionsAndTheirTags "${strAllFunctionsAndTheirTags}"
38
- local manTags=('@NAME' '@SYNOPSIS' '@DESCRIPTION' '@EXAMPLES' '@SEE_ALSO')
40
+ manTags=('@NAME' '@SYNOPSIS' '@DESCRIPTION' '@EXAMPLES' '@SEE_ALSO')
39
41
  for strFunctionAndItsTags in "${arrAllFunctionsAndTheirTags[@]}"; do
40
42
  string_split_to_array $'\n' arrFunctionAndItsTags "${strFunctionAndItsTags}"
41
43
  array_intersection manTags arrFunctionAndItsTags intersection false
42
44
  array_equals manTags intersection false
43
45
  if [[ $? -ne 0 ]]; then
44
- ((counter++))
45
- declare -p manTags intersection
46
- print_error "the comments is not the same as template for ${arrFunctionAndItsTags[-1]}"
46
+ counter=$((counter + 1))
47
+ typeset -p manTags intersection
48
+ lastFunction=""
49
+ for element in "${arrFunctionAndItsTags[@]}"; do
50
+ lastFunction="$element"
51
+ done
52
+ print_error "the comments is not the same as template for ${lastFunction}"
47
53
  fi
48
54
  done
49
55
 
@@ -64,7 +70,7 @@ function doc_lint_script_comment() {
64
70
  # doc_comment_to_markdown src/reflection.sh docs/references.md
65
71
  # @SEE_ALSO
66
72
  # doc_lint_script_comment
67
- function doc_comment_to_markdown() {
73
+ doc_comment_to_markdown() {
68
74
  local fromShellFile="$1"
69
75
  local toMarkdownFile="$2"
70
76
 
@@ -83,6 +89,6 @@ function doc_comment_to_markdown() {
83
89
  string_replace_regex '^(\*\*)' "- \1"
84
90
  )"
85
91
 
86
- echo -e "@NAME\n${md//${NEW_LINE_SED}/\\n}" | #NEW_LINE_SED is not compatible by pandoc
92
+ printf '%b\n' "@NAME\n${md//${NEW_LINE_SED}/\\n}" | # NEW_LINE_SED is not compatible by pandoc
87
93
  string_replace_regex '@([A-Z]+)' "##### \1" >"${toMarkdownFile}"
88
94
  }
package/src/out.sh CHANGED
@@ -14,9 +14,9 @@ LOG_LEVEL=${LOG_LEVEL:-$LOG_LEVEL_INFO} # the default value INFO will be used if
14
14
  # print_info "my message"
15
15
  # @SEE_ALSO
16
16
  # print_header, print_error, print_success, print_warn, print_args, print_info
17
- function print_debug() {
17
+ print_debug() {
18
18
  if [[ $LOG_LEVEL -le $LOG_LEVEL_DEBUG ]]; then
19
- echo -e "${COLOR_GRAY}DEBUG: ${1-$(cat)}${COLOR_END}"
19
+ printf '%b\n' "${COLOR_GRAY}DEBUG: ${1-$(cat)}${COLOR_END}"
20
20
  fi
21
21
  }
22
22
 
@@ -30,9 +30,9 @@ function print_debug() {
30
30
  # print_info "my message"
31
31
  # @SEE_ALSO
32
32
  # print_header, print_error, print_success, print_warn, print_args, print_debug
33
- function print_info() {
33
+ print_info() {
34
34
  if [[ $LOG_LEVEL -le $LOG_LEVEL_INFO ]]; then
35
- echo -e "${COLOR_END}INFO: ${1-$(cat)}"
35
+ printf '%b\n' "${COLOR_END}INFO: ${1-$(cat)}"
36
36
  fi
37
37
  }
38
38
 
@@ -46,9 +46,9 @@ function print_info() {
46
46
  # print_warn "my message"
47
47
  # @SEE_ALSO
48
48
  # print_header, print_error, print_success, print_info, print_args, print_debug
49
- function print_warn() {
49
+ print_warn() {
50
50
  if [[ $LOG_LEVEL -le $LOG_LEVEL_WARN ]]; then
51
- echo -e "${COLOR_BOLD_YELLOW}WARN: ${1-$(cat)} ${COLOR_END}"
51
+ printf '%b\n' "${COLOR_BOLD_YELLOW}WARN: ${1-$(cat)} ${COLOR_END}"
52
52
  fi
53
53
  }
54
54
 
@@ -62,9 +62,9 @@ function print_warn() {
62
62
  # print_error "my error message"
63
63
  # @SEE_ALSO
64
64
  # print_header, print_success, print_warn, print_info, print_args, print_debug
65
- function print_error() {
65
+ print_error() {
66
66
  if [[ $LOG_LEVEL -le $LOG_LEVEL_ERROR ]]; then
67
- echo -e "${COLOR_BOLD_RED}ERROR: ${1-$(cat)} ${COLOR_END}"
67
+ printf '%b\n' "${COLOR_BOLD_RED}ERROR: ${1-$(cat)} ${COLOR_END}"
68
68
  fi
69
69
  }
70
70
 
@@ -78,9 +78,9 @@ function print_error() {
78
78
  # print_success "my message"
79
79
  # @SEE_ALSO
80
80
  # print_header, print_error, print_warn, print_info, print_args, print_debug
81
- function print_success() {
81
+ print_success() {
82
82
  if [[ $LOG_LEVEL -le $LOG_LEVEL_WARN ]]; then
83
- echo -e "${COLOR_BOLD_GREEN}OK: ${1-$(cat)} ${COLOR_END}"
83
+ printf '%b\n' "${COLOR_BOLD_GREEN}OK: ${1-$(cat)} ${COLOR_END}"
84
84
  fi
85
85
  }
86
86
 
@@ -96,12 +96,23 @@ function print_success() {
96
96
  # print_args var1 var2
97
97
  # @SEE_ALSO
98
98
  # print_header, print_error, print_success, print_warn, print_info, print_debug
99
- function print_args() {
99
+ print_args() {
100
100
  if [[ $LOG_LEVEL -le $LOG_LEVEL_WARN ]]; then
101
101
  local varName varValue varValueOutput
102
102
  for varName in "$@"; do
103
- varValue=$(eval echo '$'"${varName}")
104
- varValueOutput=$([[ -z "${varValue}" ]] && echo "<EMPTY>" || echo "${COLOR_BLUE}${varValue}${COLOR_END}")
103
+ eval 'varValue="${'"$varName"'-}"'
104
+ if [[ -n "${BASH_VERSION:-}" ]]; then
105
+ # Historical Bash print_args used `eval echo $name`, which
106
+ # normalized whitespace and echo options in displayed values.
107
+ # Echo expanded DATA, never eval it as new shell source.
108
+ # shellcheck disable=SC2086,SC2116
109
+ varValue="$(echo $varValue)"
110
+ fi
111
+ if [[ -z "$varValue" ]]; then
112
+ varValueOutput="<EMPTY>"
113
+ else
114
+ varValueOutput="${COLOR_BLUE}${varValue}${COLOR_END}"
115
+ fi
105
116
  printf "%-30.30s%s\n" "${varName}:" "${varValueOutput}"
106
117
  done
107
118
  fi
@@ -117,8 +128,8 @@ function print_args() {
117
128
  # print_header "My header1"
118
129
  # @SEE_ALSO
119
130
  # print_error, print_success, print_warn, print_info, print_args, print_debug
120
- function print_header() {
131
+ print_header() {
121
132
  if [[ $LOG_LEVEL -le $LOG_LEVEL_ERROR ]]; then
122
- echo -e "${COLOR_BOLD_MAGENTA}\n### ${1-$(cat)} ${COLOR_END}"
133
+ printf '%b\n' "${COLOR_BOLD_MAGENTA}\n### ${1-$(cat)} ${COLOR_END}"
123
134
  fi
124
135
  }
package/src/process.sh CHANGED
@@ -10,7 +10,7 @@
10
10
  # prc_filter_by_port 9090
11
11
  # @SEE_ALSO
12
12
  # prc_kill_by_port
13
- function prc_filter_by_port() {
13
+ prc_filter_by_port() {
14
14
  if [ $# -eq 0 ]; then
15
15
  lsof -iTCP -sTCP:LISTEN -n -P
16
16
  elif [ $# -eq 1 ]; then
@@ -29,7 +29,7 @@ function prc_filter_by_port() {
29
29
  # prc_kill_by_port 9090
30
30
  # @SEE_ALSO
31
31
  # prc_filter_by_port
32
- function prc_kill_by_port() {
32
+ prc_kill_by_port() {
33
33
  local port="$1"
34
34
  local signal="${2-15}"
35
35
 
@@ -52,7 +52,7 @@ function prc_kill_by_port() {
52
52
  # prc_filter_by_cmd node
53
53
  # @SEE_ALSO
54
54
  # prc_kill_by_cmd
55
- function prc_filter_by_cmd() {
55
+ prc_filter_by_cmd() {
56
56
  if [ $# -eq 0 ]; then
57
57
  ps
58
58
  elif [ $# -eq 1 ]; then
@@ -71,7 +71,7 @@ function prc_filter_by_cmd() {
71
71
  # prc_kill_by_cmd my-app
72
72
  # @SEE_ALSO
73
73
  # prc_filter_by_cmd
74
- function prc_kill_by_cmd() {
74
+ prc_kill_by_cmd() {
75
75
  local cmd="$1"
76
76
  local signal="${2-15}"
77
77