The method I'm currently using seems to be working alright in this particular case, but when I was first searching around for answers I couldn't find any questions regarding this. So, an example of how I'm checking arrays currently:
# An example array, $array_1, with three items.
set -l array_1 "item_1" "item_2" "item_3"
# How I'm checking if an array exists.
if test -n "$array_1"
echo "The array exists."
end
# How I'm checking if an array doesn't exist.
if test -z "$array_2"
echo "The array does not exist."
end
# How I'm checking if an array element exists.
if test -n "$array_1[2]"
echo "Item 2 from the array_1 exists."
end
I don't know if this is the suggested methods of checking arrays, but at the moment those methods are working in my particular cases. Is there any case in which these methods would break? And, does anyone know of a better way to achieve these checks that would be considered best practices?
Any and all input is appreciated. Hopefully, the answers to this question will help anyone else in the future looking into working with arrays in the fish shell.
Use set -q
.
if set -q array_1
echo "array_1 exists"
end
if set -q array_1[2]
echo "Item 2 from array_1 exists
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.