6 Jan 2024 |
Elsie | oh wait before that | 17:58:55 |
Elsie | declare -n cannot have the name of an already declared variable | 17:59:11 |
Elsie | local can only be used inside functions | 17:59:22 |
Elsie | same for declare -n | 17:59:28 |
Elsie | export can be used anywhere | 17:59:33 |
Elsie | foo() {} creates a function that can leak into outside foo() () creates a subshell function so nothing can be leaked | 17:59:53 |
vigress7 | So if I export foo inside bar() and then need to use it in baz() , I need declare -n foo for baz() to inherit foo ? | 18:01:51 |
Elsie | oh nono that's not how declare -n works | 18:02:14 |
Elsie | are you familiar with C pointers? | 18:02:21 |
vigress7 | Sorta | 18:02:28 |
Elsie | function change_foo() {
declare -n bar="${1}"
bar=0
}
foo=71
echo "${foo}" # Prints 71
change_foo foo
echo "${foo}" # Prints 0 | 18:03:40 |
Elsie | or lemme also get a real example in pacstall | 18:03:42 |
Elsie | It's more useful for arrays | 18:03:52 |
Elsie | https://github.com/pacstall/pacstall/blob/develop/misc/scripts/dep-tree.sh | 18:03:58 |
Elsie | oh and local -n and declare -n are the same iirc | 18:04:15 |
Elsie | and a funny thing about this is that you can do: | 18:06:11 |
Elsie | function foo() ((bar=0)) | 18:06:18 |
Elsie | that's valid bash | 18:06:25 |
Elsie | and any loop, conditional, or grouping commands are valid for that ending | 18:07:59 |
Elsie | the most common is that last one which the most common of that is {} | 18:08:13 |
vigress7 | So why does this result in no output? How does inheritance work here
function bar() {
export foo=5
}
function baz() {
echo $foo
}
baz | 18:11:17 |
vigress7 | Oh sorry | 18:11:27 |
Elsie | you never call bar | 18:11:31 |
vigress7 | Yes brainfart | 18:11:37 |
Elsie | if you did:
bar
baz | 18:11:49 |
Elsie | it would work | 18:11:50 |
Elsie | or hell even if you didn't have export in front | 18:12:07 |
Elsie | the only reason to use export is to make it available to sub-processes | 18:12:30 |
Elsie | function bar() {
export foo=5
}
function baz() (
echo $foo
)
bar
baz | 18:12:59 |
vigress7 | Then why, when I edit the pacscript to export mod_dir in package() , does post_install() not inherit mod_dir ? | 18:13:02 |