Bash "Declare -A" Does Not Work on MACos

Bash declare -A does not work on macOS

declare -A (associative arrays) are a bash 4+ feature.

The OS X bash is likely 3.X.

I don't know that OS X has an official update for bash 4+.

brew/etc. might though.

Bash associative dictionary not working (declare: -A: invalid option)

The problem was fixed by upgrading bash. $BASH_VERSION prints your true version

brew unlink bash
brew update && brew install bash

test.sh:

declare -A instance_map=( ["dev"]="project-dev" ["stage"]="project-staging" ["prod"]="project" )

echo ${instance_map["dev"]}
echo ${instance_map["stage"]}

Associative arrays: error declare: -A: invalid option

Make sure the version of bash being invoked as interpreter at the top of your shell script (#!/bin/bash or whatever) is also version 4. If you're doing:

bash --version

and it's giving you v4, do a which bash to check it's location.

I can't use bash 5 syntax even after configuring it in MacOSX

The bash -version (or bash --version) command tells you the version of Bash that you would get if you ran bash.

But I'm guessing that you're not manually running bash; rather, you're just opening the shell, which is probably still the older version of Bash. (It's quite possible, and not even that unusual, to have two copies of Bash installed on a system in different locations.) To check this, you can run echo "$BASH_VERSION" to see the version of Bash that you're actual typing in.

To fix this, you will need to configure your machine to use the newer version of Bash as your shell. (Or you can just run bash manually.)

Unable to set variables in bash script

Assignment in bash scripts cannot have spaces around the = and you probably want your date commands enclosed in backticks $():

#!/bin/bash
folder="ABC"
useracct='test'
day=$(date "+%d")
month=$(date "+%B")
year=$(date "+%Y")
folderToBeMoved="/users/$useracct/Documents/Archive/Primetime.eyetv"
newfoldername="/Volumes/Media/Network/$folder/$month$day$year"
ECHO "Network is $network" $network
ECHO "day is $day"
ECHO "Month is $month"
ECHO "YEAR is $year"
ECHO "source is $folderToBeMoved"
ECHO "dest is $newfoldername"
mkdir $newfoldername
cp -R $folderToBeMoved $newfoldername
if [-f $newfoldername/Primetime.eyetv]; then rm $folderToBeMoved; fi

With the last three lines commented out, for me this outputs:

Network is 
day is 16
Month is March
YEAR is 2010
source is /users/test/Documents/Archive/Primetime.eyetv
dest is /Volumes/Media/Network/ABC/March162010

Using associative arrays in a bash script on MacOS

Please save this as theScript if you want to use the homebrew bash:

#!/usr/local/bin/bash
echo $BASH_VERSION
declare -A svcCount

Then make it executable with:

chmod +x theScript

and run with:

./theScript

Bash script on MacOS

Check the bash version. Associative arrays with -A were added in bash version 4.

To get it to work with version 3 you could replace the array with a backup helper function that gets called for each set of paths:

#!/bin/bash

backup() {
local dest_dir="$1"
shift
local srcs=("$@")
mkdir -p "$dest_dir"
rsync -avuz --delete --delete-after --progress "${srcs[@]}" "$dest_dir"
}

backup /Users/myuser \
/Volumes/CORSAIR/articoli \
/Volumes/CORSAIR/bibliografie \
;
backup /Users/myuser/Documents \
/Volumes/CORSAIR/dialettica \
/Volumes/CORSAIR/dizionario \
;

Can't set / update IFS variable in Bash on Mac OS X

You are trying to set IFS on the same line as your echo. Do it on a separate line, as you did with Name. Also, since IFS is the characters you declare to be white space, you'll need quotes in the echo command:

IFS=','
echo "$IFS"

Problem with Splitting Up a String and Putting it Into an Array in BASH on a Mac

read only reads one line.

Use an assignment instead. When assigning to an array, you need to use parentheses after the = sign:

#!/bin/bash
disks=( $(ls /dev/disk* | grep -e 'disk[0-9]s.*' | awk '{ print $NF }') )
echo ${disks[1]}


Related Topics



Leave a reply



Submit