To demonstrate the similarity of a current carrying solenoid to a bar magnet, let us calculate axial field of a finite solenoid carrying current.
Consider: a = radius of solenoid
2l = length of solenoid with centre O
n = number of turns per unit length
I = current passing through solenoid
OP = r
Consider a small element of thickness dx of solenoid at distance x from O. and number of turns in element = n dx.
We know magnetic field due to n turns coil at axis of solenoid is given by
It is clear from the above expression that magnetic moment of a bar magnet is equal to the magnetic moment of an equivalent solenoid that produce the same magnetic field.