Shares in UBS on Thursday extended gains as investors expected the settlement of a damaging U.S. tax dispute to allow the bank to focus on becoming profitable again.

UBS, the world's second-largest wealth manager in terms of managed assets, and the U.S. government agreed on Wednesday to settle a long-running dispute over the disclosure of names of wealthy American clients suspected of tax evasion.

The stock is benefiting from the fact Switzerland and the U.S. have reached a fundamental agreement in the tax dispute, a trader said. The details of the deal are still missing but it is definitely positive that it did not come to a trial against UBS.

The deal removes the prospect of a tax trial against UBS. A lengthy court battle would have hindered the bank's ability to win back client confidence after the subprime crisis and the tax row triggered billions of Swiss francs of client withdrawals.

This deal obviously closes a Pandora's Box for UBS, which avoids an extremely long and delicate court procedure which could have called into question its very existence, said Geneva-based lawyer Philippe Fisher.

Traders also said the prospect that the Swiss government would quickly sell its 9 percent stake in UBS was also supporting shares as a sale would signal Berne believes Switzerland's largest bank is on the right path.

The government last year injected 6 billion francs to help the bank recover from its subprime binge.

Swiss Finance Minister Hans-Rudolf Merz said earlier this week the government wants to sell the stake soon. He had previously said the government would sell its holdings when the bank was back in good shape.

UBS shares traded 4.8 percent higher at 0919 GMT, outperforming a 2 percent rise in the Dow Jones Stoxx European banks index <.SX7P>. The stock rallied 3 percent in the aftermath of the settlement on Wednesday.

Domestic rival Credit Suisse gained 2.3 percent.

The share is getting toward the level at which the government can sell its stake. Even if it sells at a lower price, it still gets the coupon, said Sarasin analyst Rainer Skierka, adding the government would still break even at a price of 12.50 Swiss francs a share.

I would not be surprised to see the government wait until the share is nearer 18 francs as this is easier to explain to the public, he said.

The government would probably favor large institutional investors who would not feed the shares back into the market, hitting the share price.

The settlement is expected to involve transferring of some UBS client names to U.S. tax authorities, but no large payment for UBS.

I don't think that a heavy fine for UBS is on the cards, said Ivan Pictet, a senior partner at Swiss private bank Pictet Cie and president of the Geneva Place Financiere.

UBS's problems mean it has missed out on much of the recovery in banking stocks this year, with its shares gaining just 10 percent so far in 2009, against a rise of around 46 percent in the shares of European peers.

Analysts at Zuercher Kantonalbank welcomed the heading off of a trial against UBS, but said the extent of client data and how it would be handed over could affect the future performance of UBS shares.

Until then the share price could remain volatile, they said in a note.

UBS reported a second-quarter loss of 1.4 billion francs and nearly 40 billion francs of outflows at its wealth and asset management divisions. But stripped of one-off charges, the bank was able to report its first pre-tax profit in two years.

(Reporting by Jason Rhodes, additional reporting by Stephanie Nebehay in Geneva; Editing by David Cowell)