!MiPOrXJIJudCrKmvfO:matrix.org

general

223 Members
11 Servers

Load older messages


SenderMessageTime
6 Jan 2024
@_discord_788875075847454742:t2bot.ioElsie oh wait before that 17:58:55
@_discord_788875075847454742:t2bot.ioElsie declare -n cannot have the name of an already declared variable 17:59:11
@_discord_788875075847454742:t2bot.ioElsie local can only be used inside functions 17:59:22
@_discord_788875075847454742:t2bot.ioElsie same for declare -n 17:59:28
@_discord_788875075847454742:t2bot.ioElsie export can be used anywhere 17:59:33
@_discord_788875075847454742:t2bot.ioElsie foo() {} creates a function that can leak into outside
foo() () creates a subshell function so nothing can be leaked
17:59:53
@_discord_1174247883814486048:t2bot.iovigress7 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
@_discord_788875075847454742:t2bot.ioElsie oh nono that's not how declare -n works 18:02:14
@_discord_788875075847454742:t2bot.ioElsie are you familiar with C pointers? 18:02:21
@_discord_1174247883814486048:t2bot.iovigress7 Sorta 18:02:28
@_discord_788875075847454742:t2bot.ioElsie
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
@_discord_788875075847454742:t2bot.ioElsie or lemme also get a real example in pacstall 18:03:42
@_discord_788875075847454742:t2bot.ioElsie It's more useful for arrays 18:03:52
@_discord_788875075847454742:t2bot.ioElsie https://github.com/pacstall/pacstall/blob/develop/misc/scripts/dep-tree.sh 18:03:58
@_discord_788875075847454742:t2bot.ioElsie oh and local -n and declare -n are the same iirc 18:04:15
@_discord_788875075847454742:t2bot.ioElsie and a funny thing about this is that you can do: 18:06:11
@_discord_788875075847454742:t2bot.ioElsie
function foo() ((bar=0))
18:06:18
@_discord_788875075847454742:t2bot.ioElsie that's valid bash 18:06:25
@_discord_788875075847454742:t2bot.ioElsie and any loop, conditional, or grouping commands are valid for that ending 18:07:59
@_discord_788875075847454742:t2bot.ioElsie the most common is that last one which the most common of that is {} 18:08:13
@_discord_1174247883814486048:t2bot.iovigress7 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
@_discord_1174247883814486048:t2bot.iovigress7 Oh sorry 18:11:27
@_discord_788875075847454742:t2bot.ioElsie you never call bar 18:11:31
@_discord_1174247883814486048:t2bot.iovigress7 Yes brainfart 18:11:37
@_discord_788875075847454742:t2bot.ioElsie if you did:
bar
baz
18:11:49
@_discord_788875075847454742:t2bot.ioElsie it would work 18:11:50
@_discord_788875075847454742:t2bot.ioElsie or hell even if you didn't have export in front 18:12:07
@_discord_788875075847454742:t2bot.ioElsie the only reason to use export is to make it available to sub-processes 18:12:30
@_discord_788875075847454742:t2bot.ioElsie
function bar() {
  export foo=5
}

function baz() (
  echo $foo
)

bar
baz
18:12:59
@_discord_1174247883814486048:t2bot.iovigress7 Then why, when I edit the pacscript to export mod_dir in package(), does post_install() not inherit mod_dir? 18:13:02

Show newer messages


Back to Room ListRoom Version: 6