Don't use '$mdadm', just 'mdadm'. And --stop all arrays when finished.
If you have stacked arrays, then mdadm -As --homehost=fred should work but doesn't. It gets into an infinite loop! So write some tests, and fix the bugs.