أَخْبَرَنَا بِشْرُ بْنُ ثَابِتٍ الْبَزَّارُ، حَدَّثَنَا حَسَّانُ بْنُ مُسْلِمٍ، عَنْ يُونُسَ بْنِ عُبَيْدٍ، عَنْ الْحَسَنِ، قَالَ: "لَقَدْ طَلَبَ أَقْوَامٌ الْعِلْمَ، مَا أَرَادُوا بِهِ اللَّهَ تعَالَيَ وَلَا مَا عِنْدَهُ، قَالَ: فَمَا زَالَ بِهِمْ الْعِلْمُ، حَتَّى أَرَادُوا بِهِ اللَّهَ وَمَا عِنْدَهُ".
Imam Hasan al-Basri (may Allah have mercy on him) said: People sought knowledge even though, by means of it, they did not intend Allah nor what is with Allah. He said: They continued to acquire knowledge until their intention became sincere, and they intended Allah and what is with Allah.
Brief Explanation
(Commentary on Hadiths 368 to 371)
From these narrations, it is established that even if, in the beginning, there is no particular objective or intention for acquiring knowledge, the light of knowledge itself creates sincerity and devotion to Allah (ikhlas wa lillahiyyat).
It should be clear that by "knowledge" here is meant the knowledge of the Book and the Sunnah.