From 02086e7115f43b96ba0e21f1d76bdee859d41db1 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 7 Oct 2024 18:51:57 -0500 Subject: [PATCH] Add Link A Scalar To An Array as a Zsh TIL --- README.md | 3 ++- zsh/link-a-scalar-to-an-array.md | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 zsh/link-a-scalar-to-an-array.md diff --git a/README.md b/README.md index ff820b3..c877ae7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1454 TILs and counting..._ +_1455 TILs and counting..._ --- @@ -1730,6 +1730,7 @@ _1454 TILs and counting..._ ### Zsh - [Add To The Path Via Path Array](zsh/add-to-the-path-via-path-array.md) +- [Link A Scalar To An Array](zsh/link-a-scalar-to-an-array.md) ## Usage diff --git a/zsh/link-a-scalar-to-an-array.md b/zsh/link-a-scalar-to-an-array.md new file mode 100644 index 0000000..7a5dfe9 --- /dev/null +++ b/zsh/link-a-scalar-to-an-array.md @@ -0,0 +1,38 @@ +# Link A Scalar To An Array + +`Zsh` has a builtin command `typeset` that does a variety of things. When given +the `-T` flag and the names of a scalar and an array, it will link them +together so that a change to one is reflected in the other. + +The scalar is a string of values delimited by a colon (`:`). The array is an +array that can be interacted with using array operations like append (`+=`). + +```bash +$ typeset -T FOO foo + +$ echo $FOO + + +$ export FOO="one:two" + +$ echo $foo +one two + +$ foo+=three + +$ echo $FOO +one:two:three +``` + +Notice `FOO` is initially empty. I then `export` it to overwrite it with two +values delimited by a colon. Since `foo` is automatically kept in sync, I can +`echo $foo` and see those values displayed as an array. I can then append a +third value using an array operation on `foo`. The update will be automatically +reflected in `FOO`. + +`Zsh` does this under the hood for `PATH` and `path` which is why you can [add +to the path via the path array](add-to-the-path-via-path-array.md). + +See `man zshbuiltins` for more details. + +[source](http://devlib.symbian.slions.net/s3/GUID-D87C96CE-3F23-552D-927C-B6A1D61691BF.html)