It kind of depends on which bear, black bears are one thing and brown bears another.
And lets say that you have actually taken all the precautions to not attract a bear in your camp in first place, a bear wont be going through your pack in search of your iPod.
First be aware of your surroundings when you set up camp, so you know where to move. Give a look around as there might be cubs, you don’t want to move towards those. If you can walk away do so, slowly, walking not running and always face the animal, be careful where you put your feet, you don’t want to trip and fall. Keep your deterrent ready for use. Do not move in a direction that would make the animal think you are gonna take/protect a source of food. When you first see the animal don’t make sudden movements or noises, if surprised it will go on the defensive.
If its a black bear he might already decide to move out seeing you, if you want to try to make him move out then the idea is to make yourself look bigger and make a lot of noise. Keep deterrent ready.
Before deciding to move any bear out of camp make really sure it has an escape route, if the animal feels trapped things wont go well. Usually black bears wont test you unless its a female that is trying to defend their cubs.
They normally don’t see people as food.
Brown bears instead are a whole different story, the theory goes as above but forget making them move out of camp and to that you add the probable play dead emergency thing, it seems the problem with that is that people don’t wait long enough before restarting moving and the animal comes back but I’m not sure how much one can actually play dead waiting for their soiled pants to hit the fan.
Obviously the best thing is to not have the bear in camp, no food should stay in the camp, same goes for soap, toothpaste, toiletries etc. For the same reason, food that cant be prepared in a "clean" way shouldn’t be prepared at or near the camp (fish cleaning for example), don’t smear food on you (your clothes aren’t a napkin for cleaning bacon grease from your fingers) etc etc.
Give a look around the camp area for what people camping there might have left/buried, clean that up if you can. (lots of people think that since they are leaving they can be sloppy)
A bit of reading:
As for deterrents: bear spray!!!
Guns yes, they are guns etc etc but: unless you shoot and kill things can go downhill fast. Plenty of guns enthusiasts handle guns and know how to shoot at a target, hunters are used to the gun etc. All more or less controlled situations, a bear coming at you is way different matter, not easy to keep calm. Not all guns would be effective on a bear so you have to consider that too. If you camp with family and friends or all have a gun and know how to use it under pressure or its pointless, many times one has a gun the rest nothing, he cant be guarding both camp and somebody that is taking a bathroom break and if something happens to that person its done. Bear spray is inexpensive enough that everyone in the group can have a can, even a kid can carry it and learn to use it properly. It doesn’t kill the animal either (and if a nervous trigger happy element accidentally sprays another member of the group is not a disaster like if they had a gun)
If you read the accident reports there have been people defending themselves with guns and it worked, but usually they had time to do so and they were experienced. In many cases people don’t have the time to get the gun, and if they did they didn’t have the time or capacity to aim it properly. With the spray there have been also accidents were they had it and didn’t have the time to get at it, but in the cases they got it ready it worked because it was just point towards the animal -> shoot.
Another way is to bring camping someone you don’t like and slower than you ;)