Wayne Stilwel, 25, was caught on CCTV committing the act at Edinburgh's Central Mosque on January 31 this year.
The city's sheriff court heard that Islam prohibits the consumption of pork and that Stilwel's actions offended people at the mosque.
Stilwel, a prisoner of HMP Edinburgh, pled guilty to a breach of the peace charge before Sheriff Gordon Liddle at a hearing last month.
Sentence had been deferred in order for the court to obtain reports about Stilwel's character.
Sheriff Gordon Liddle yesterday branded Stilwel's actions "grossly offensive" and said he had no option but to send him to prison.
Sheriff Liddle added: "A custodial sentence is inevitable."
During the earlier court appearance, the court heard from depute procurator fiscal Ian Wallace who said that police were called to the mosque following the crime. Mr Wallace told the court that police later traced Stilwel and arrested him.
Mr Wallace also said that Stilwel told police that he was aware that he had caused offence to followers of Islam.
Mr Wallace added: "He said he was aware of the significance of the bacon."
Defence solicitor Matthew Nicholson told the court that his client was sorry for his crime. He added: "He fully realises that he will receive a custodial sentence."
Sheriff Liddle told Stilwel: "Your actions were grossly offensive."