From 737340a77bd49c0446e6f1a7f119700987254715 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Mon, 12 Jun 2023 07:27:10 +0100 Subject: [PATCH] bash: notes on variable indirection --- .../Shell/Variable_indirection.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Programming_Languages/Shell/Variable_indirection.md diff --git a/Programming_Languages/Shell/Variable_indirection.md b/Programming_Languages/Shell/Variable_indirection.md new file mode 100644 index 0000000..a445c03 --- /dev/null +++ b/Programming_Languages/Shell/Variable_indirection.md @@ -0,0 +1,51 @@ +--- +categories: + - Programming Languages +tags: + - shell +--- + +# Variable indirection + +> Write proper notes on this + +```sh +function array_empty() { + declare -n arr=$1 + # Proceed if array not empty: + if [ ${#arr[@]} -gt 0 ]; then + return 1 # array is not empty + else + return 0 # array is empty + fi +} + +my_array=(1 2 3) + +function push() { + # $1 = stack name + local stack_name="$1" + if [ "${stack_name}" ]; then # stack exists + if array_empty "${!stack_name}"; then + echo "stack is empty" + else + echo "stack is not empty" + fi + else + echo "Error: ${stack_name} does not exist." + fi +} + +push "my_array" + +``` + +In Bash scripting, ${!stack_name} is an example of indirect variable referencing or variable indirection. It's a way to use a variable whose name is stored in another variable. + +Let's break it down: + +`${stack_name}`: This syntax is used to reference the value of a variable. So if stack_name="my_array", then ${stack_name} would return "my_array". + +`${!stack_name}`: Adding the ! introduces indirection. Now, instead of getting the value of stack_name, you get the value of the variable whose name is stored in stack_name. So if stack_name="my_array" and my_array=(1 2 3), then ${!stack_name} would return (1 2 3). + +So, in the context of your script, `${!stack_name} allows you to pass the name of an array (as a string) to your push function, and then use that string to indirectly reference the actual array within the function.