Space is full of potential dangers—rogue comets, Martians, bags full of astronaut poop, you name it. But if it really came down to it, and you did have to defend yourself against an interstellar attacker, would a traditional gun actually be able to do the trick?
As it turns out, the vacuum of outer space would not be a problem in terms of firing a bullet. After all, guns don't technically need oxygen to work. The gunpowder (or whichever explosive is in the cartridge holding the bullet) is completely self-contained and does not depend on the surrounding atmosphere. It has an oxidizer mixed with the fuel and is perfectly capable of firing in a vacuum. Even the primer, which is struck by the pin of the firearm, is completely self-contained and will work in a vacuum.
In fact, the gun will work even work ever-so-slightly better in outer space, because the bullet won't have to push and compress the air in the barrel as it exits the gun. Since there's no air to slow down the bullet as it travels, the range of the gun would essentially be infinite. Of course, the the trajectory would curve eventually, since it would probably still be in orbit, but it would have a notably different orbit than the gun itself or the person that fired it.
For example, the International Space Station (ISS) has an orbital speed of roughly 17,000 miles per hour which is 7,600 meters per second. Muzzle velocities range from about 120 meters per second to about 1,200 meteres per second, depending on the gun. Therefore, the orbit of the bullet will differ from the orbit of the astronaut that fired it. In general, firing anywhere in the forward direction of the orbit will result in a more eccentric orbit that will always stay at or above the ISS's orbit. If it's fired against the direction of the orbit, the bullet may end up dipping into the atmosphere and therefore de-orbiting.
There is no need to "push" against anything for a gun to work. The difference between the mass of the bullet and the mass of the gun—plus the person holding the gun—will ensure that the bullet gets almost all the kinetic energy of the explosive (even though they both get the same momentum due to conservation of momentum).
However, assuming the astronaut is floating freely in space, if the line of the barrel does not point through the center of mass of the gun/astronaut, the firing of the gun will impart some small angular momentum to the astronaut.
To do some concrete calculations, the M4 carbine (above) has a muzzle velocity of 910 m/s. the gun weighs 3.4 kg and the bullet weighs 4g. The ISS extravehicular space suit weighs about 124 kg and let's assume the astronaut weighs 70 kg. Then the weight of the gun, astronaut and space suit is about 197 kg. Given the muzzle velocity of 910 m/s, the bullet will have a momentum of 3.6 Ns (see 4 g * 910 m/s). For the astronaut+gun to have the same momentum, it will move at 18 mm/s (see 4 g * 910 m/s /197 kg), so the astronaut will have a very low velocity. The kinetic energy of the bullet will be 1656 J (see 4 g * (910 m/s)^2/2) whereas the astronaut+gun will have a kinetic energy of 0.02 J (see 122 kg * (18 mm/s)^2/2), so as promised almost all the kinetic energy is in the bullet.
In the worst case scenario, if the gun was fired from near the astronaut's head, the astronaut might end up rotating about once every 3 minutes which could be easily corrected by the thrusters the astronauts use to move about.
So in short, yes, you can shoot a gun in space. But for everyone's sake—let's just hope it never has to come to that.
Top image inspired by Shutterstock/iurii
This answer has been lightly edited for grammar and clarity.