Gallagher, who was on early release from a four-year jail sentence for a serious assault with scissors, left spots of his own blood at the scene of the break-in and a DNA match for him was found.

A judge told Gallagher at the High Court in Edinburgh: “At the age of 37 you have an extraordinary offending history with a total of some 90 previous convictions.”

Lady Scott pointed out that although he had 16 offences of housebreaking on his record it also included convictions for violence among them assault and robbery and assault to severe injury, permanent impairment and disfigurement.

“Your conduct must have been terrifying for your elderly victim and it has left him anxious and distressed,” said the judge.

Lady Scott said Gallagher had been “surrounded” by drug and alcohol abuse since childhood and added his own offending was linked to drink and drug abuse.

The judge told him she would have jailed him for six years, but for his guilty plea in the case. She also ordered that he be under supervision for a further three years.

Unemployed Gallagher, who has been recalled to prison after his licence was revoked, earlier admitted assaulting and robbing the man, who was 90 at the time, on October 26 last year after forcing entry to his home.

The court heard that the victim had gone to bed in his home in a quiet cul-de-sac after securing windows and doors.

But about 15 minutes later he woke up after the intruder demanded his money and assaulted him. The victim was taken to Edinburgh Royal Infirmary and found to have bruising but no evidence was discovered of further head injury.

After the DNA match identified Gallagher as the perpetrator he was detained by police in January this year and taken to St Leonard’s police station and interviewed but made no comment to every question he was asked.

Defence solicitor advocate Brian Gilfedder said Gallagher, a prisoner in Saughton, had “genuinely thought the house was empty”.

He said: “He had no idea that there was an elderly person in the house. He is obviously ashamed of what he did.”