-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformations
More file actions
executable file
·74 lines (63 loc) · 3.36 KB
/
transformations
File metadata and controls
executable file
·74 lines (63 loc) · 3.36 KB
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
#!/bin/bash
# shellcheck disable=SC2016
# TODO: Add function to colorize bash syntax in output
printf "--------------- Parameter Transformations ---------------\n"
cat <<- 'EOC'
Pass arguments to this script to see how they will transform.
Parameter Transformation Operators (on single variables)
${var@U} - Convert everythings to uppercase.
${var^^} - Convert everythings to uppercase.
${var@u} - Capitalizes the first character.
${var^} - Capitalizes the first character.
${var@L} - Converts everything to lowercase.
${var,,} - Convert everythings to lowercase.
${var,} - Converts first character to lowercase.
${var@Q} - Quotes the value, making it safe to reuse as input.
${var@E} - Expands escape sequences (like $'...' syntax).
${var@P} - Expand as a prompt string (PS1).
${var@A} - Returns an assignment statement to recreate the variable with its attributes.
${var@K} - Produces a quoted version of the value, displaying arrays as key-value pairs.
${var@a} - Returns the variable's attribute flags (like readonly, exported).
Using transformations on arrays:
${@^} - Converts the first character of each parameter to uppercase.
${*^} - Converts the first character of the combined string to uppercase
${@^^} - Converts all characters to uppercase, as separate items
${*^^} - Converts all characters to uppercase as one string
${@,} - Lowercases the first character of each parameter
${*,} - Lowercases only the first character of the combined string
Using the letters (requires the @):
${@@Q} - Quotes each parameter individually
${*@Q} - Quotes the entire combined string
${@@A} - Returns an assignment statement to recreate the array.
${*@A} - Returns an assignment statement to recreate the string.
Arrays tl;dr:
$@ / ${@} applies to each individually
$* / ${*} combines and applies to the combined string.
[@] and [*] will only use values in dictionaries. Use ${!arr[@|*]} to use keys.
EOC
{ [[ -z $1 ]] &&
printf "No arguments found for examples. Exiting.\n" && exit 0;
} || {
: "${arg:=$1}"
}
printf -- "--------------- First Argument ---------------\n"
printf '${arg@L} - lowercase: %s\n' "${arg@L}"
printf '${arg@Q} - quoted: %s\n' "${arg@Q}"
printf '${arg@K} - quoted, key/value: %s\n' "${arg@K}"
printf '${arg@a} - attributes: %s\n' "${arg@a}"
printf '${arg@u} - TitleCase: %s\n' "${arg@u}"
printf '${arg@U} - Uppercase: %s\n' "${arg@U}"
printf -- "------------- Array of Arguments -------------\n"
printf '${@^} - Capitalize each parameter: %s\n' "${@^}"
printf '${*^} - Capitalize only the first letter of the combined string: %s\n' "${*^}"
printf '${@^^} - Uppercase each parameter completely: %s\n' "${@^^}"
printf '${*^^} - Uppercase the entire combined string: %s\n' "${*^^}"
printf '${@,} - Lowercase the first character of each parameter: %s\n' "${@,}"
printf '${*,} - Lowercase only the first character of the combined string: %s\n' "${*,}"
printf '${@,,} - Lowercase all characters of each parameter: %s\n' "${@,}"
printf '${*,,} - Lowercase all characters of the combined string: %s\n' "${*,}"
printf '${@@Q} - Quote each parameter individually: %s\n' "${@@Q}"
printf '${*@Q} - Quote the entire combined string: %s\n' "${*@Q}"
printf '${*@Q} - Quote the entire combined string: %s\n' "${*@Q}"
printf '${@@A} - Return an array that contains statements to recreate each variable: %s\n' "${@@A}"
printf '${*@A} - Return an assignment statement to recreate the variables: %s\n' "${*@A}"