Search code examples
bashreferencedeclare

How to use namerefs in Bash?


How do I use namerefs in Bash? I'm a beginner and I have no idea what I'm doing wrong.

My code is just this:

#!/bin/bash

declare -n foo="bar"

And I immediately get the following error when I run it:

declare: -n: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

I couldn't find anything online to solve this problem.


Solution

  • Solved! I just had to upgrade my Bash to the newest version (for me, 5.0.16) and it now works beautifully. Really weird that the latest macOS ships with a painfully outdated version (for me, 3.2.57).