It is not permissible to take loans on the basis of riba, whether to buy shares or anything else, because riba is haraam and abhorrent and reprehensible, and is a major sin.
Allaah says (interpretation of the meaning):
“O you who believe! Fear Allaah and give up what remains (due to you) from Ribaa (from now onward) if you are (really) believers.
279. And if you do not do it, then take a notice of war from Allaah and His Messenger but if you repent, you shall have your capital sums. Deal not unjustly (by asking more than your capital sums), and you shall not be dealt with unjustly (by receiving less than your capital sums)”
And Muslim (1518) narrated that Jaabir (may Allaah be pleased with him) said: The Messenger of Allaah (peace and blessings of Allaah be upon him) cursed the one who consumes riba and the one who pays it, the one who writes it down and the two who witness it, and he said: they are all the same.
The one who does that must repent to Allaah, may He be exalted, by regretting what has passed and resolving not to do it again in the future. He does not have to do any more than repay the capital, unless he fears harm, in which case he may also pay the haraam interest.
If you can hasten to pay it off and get out of dealing with the bank, that is better.
If the shares are pure, then you do not have to sell them; you can keep them even if you bought them with a riba-based loan, as long as you repent to Allaah from riba as stated above.
If the shares are haraam or mixed, then you have to get rid of them. See the answer to question no. 45319.
To sum up: If a person buys something permissible with a riba-based loan, he has to repent to Allaah, and there is nothing wrong with him keeping or making use of the permissible thing, and he does not have to sell it to pay off the loan; rather he should pay off the loan at the appointed time, but if he hastens to pay it off, that is better.
And Allaah knows best.