So I have a loop that basically goes through all of the disks installed on the system, then it assigns a variable to a disk name. however, I cannot use those variables if it's not inside the loop, how do I make them available to be used in other functions or other parts of the script?
Here is the code
#!/bin/bash
dev=1
for disk in $(fdisk -l | grep -o '/dev/sd[a-z]'); do
set "DISK$dev=$disk"
dev=$((dev+1))
done
So if I do echo $DISK1 for example, it doesn't display anything.
But if I do echo $DISK1 INSIDE the loop, then ir displays the fist variable assignment. Can I export them and make them available outside of the loop?
DISK1, not this loop. Yoursetcommand would set the first positional parameter to the valueDISK1=/dev/sda(or something similar); it would not define a variable namedDISK1.set. The linked duplicate tells you the proper (non-set) tools to use to solve the same problem (the OP there is initially trying to useeval, but the end purpose is the same). I don't see how it doesn't apply.