Bash Variable Array, Trying to add another value into the array
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash Variable Array, Trying to add another value into the array
Hi All,
Just thinking about a variable array and i'd like to if possible...
when working with an array simply add a value to the array at the next available slot/number so to speak.
IE
i have an array:-
Code:
array=( `echo 1 2 3 4` )
this obviously giving me 4 values in my array
Code:
echo ${#array[@]}
4
Instead of then having to do something like:-
Code:
number=`echo ${#array[@]}`
((number++)) #add one
array[${number}]=5
Is it possible to not have to know the next number in the array so the value can just be added to the next available slot in the array so to speak. ie
Code:
array[next_availble_number]=5
rather than going through the rigmarole of all that above. It would be much nicer if its possible to just say chuck this value anywhere in the array as i don't care where it goes. Just as long as its not overwriting something already in the array, and is using the next available number in the array so that when you do something like:-
Code:
echo ${#array[@]
or
Code:
echo ${array[@]
you still get returned the correct numbers of 5 and 1 2 3 4 5
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.