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.
What would be the equivalent for a counter like so
Code:
for (( j = 0; j < ${#array[@]}; j++ ))
do
printf "[%d] %s\n" $j "${anciator[$j]}"
done
I wonder whether using a counter as above could fail. The most secure way for printing seems to involve use of "${anciator[@]}" or "${!anciator[@]}", rather than the above.
A=(One Two Three Four)
B=(Cat Dog Cow Bird)
#Alternate items from 2 arrays
for i in "${!A[@]}"; do
echo -e ""${A[i]}"\n"${B[$i]}""
done
echo -e ''$_{1..50}'\b─'
for ((j = 0; j < ${#A[@]}; j++)); do
echo -e ""${A[j]}"\n"${B[$j]}""
done
Interesting. I don't think that I noticed that before.
Code:
A=(One Two Three Four)
for ((j = 0; j < ${#A[@]}; j++)); do
echo -e "${A[$j]}"
done
One
Two
Three
Four
unset A[1]
for ((j = 0; j < ${#A[@]}; j++)); do
echo -e "${A[$j]}"
done
One
Three
for i in "${!A[@]}"; do
echo -e "${A[$i]}"
done
One
Three
Four
no, please do not use bash, it is not capable to do advanced handling of anything. It is not meant to do that.
Have been thinking about your comment recently and plan to do some things in `perl`. Still for some of the things I currently have, an implementation in bash, even though crude, is good to have.
A=(One Two Three Four five Six Seven Eight)
for key in "${!A[@]}"; do
echo "Key "$key" is Value "${A[$key]}""
done
unset A[3]
for key in "${!A[@]}"; do
echo "Key "$key" is Value "${A[$key]}""
done
B=(One Two Three Four five Six Seven Eight)
for ((j = 0; j < ${#B[@]}; j++)); do
echo "Key "$j" is Value "${B[$j]}""
done
unset B[3]
for ((j = 0; j < ${#B[@]}; j++)); do
echo "Key "$j" is Value "${B[$j]}""
done
A=(One Two Three Four)
unset A[1]
set -xv
for ((j = 0; j < ${#A[@]}; j++)); # ${#A[@]} is the number of elements, it is now 3
do
echo -e "${A[$j]:-missing}";
done
+ (( j = 0 )) # j=0 initialization
+ (( j < 3 )) # j < ${#A[@]}, do the cycle
+ echo -e One # print A[0]
One
+ (( j++ )) # increment j (j is now 1)
+ (( j < 3 )) # j < ${#A[@]}, do the cycle
+ echo -e 'missing' # print A[1]
missing # We have no A[1], it was unset
+ (( j++ )) # increment j (j is now 2)
+ (( j < 3 )) # j < ${#A[@]}, do the cycle
+ echo -e Three # print A[2]
Three
+ (( j++ )) # increment j (j is now 3)
+ (( j < 3 )) # j < ${#A[@]} is false now, end of loop, exit
# j = 3 # skipped
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.