The only rules a bag of holding has on anything exiting it is:
Retrieving a specific item from a bag of holding is a move action, unless the bag contains more than an ordinary backpack would hold, in which case retrieving a specific item is a full-round action.
and
If a bag of holding is turned inside out, all of its contents spill out, unharmed, but the bag must be put right before it can be used again.
This would imply that in order for a specific thing inside the bag to leave, it must be retrieved. Anything else would require asking the GM for the specifics of how it works.
Here are some things they may want to consider though, if you exit the bag on your own:
- How do you know when to exit the bag? You cannot hear your allies as you're stuck in another dimensional space, same for sight.
- When you exit, do you fall prone from stepping into the middle of the air?
- Does this movement distract/hinder the ally who's carrying the bag you step out from?
- Are you climbing out of the bag, if so what example from the climb skill does it resemble?
As for the specifics of the attack
Per the cover rules from the combat section, in order to be able to make the ranged attack, you have to fully exit the bag and move to an unoccupied square.
To determine whether your target has cover from your ranged attack, choose a corner of your square. If any line from this corner to any corner of the target’s square passes through a square or border that blocks line of effect or provides cover, or through a square occupied by a creature, the target has cover (+4 to AC).
and
If you don’t have line of effect to your target (that is, you cannot draw any line from your square to your target’s square without crossing a solid barrier), he is considered to have total cover from you. You can’t make an attack against a target that has total cover.
Because while in the bag, you do not occupy a square in the same dimension as the target, any corner of your square you choose to initiate you attack from while in the bag grants total cover, and "You can’t make an attack against a target that has total cover."