The bunching of an electron beam by an ultra-relativistic laser pulse in vacuum is considered. The one-dimensional theory describing this process is elaborated. The laser pulse is shown to compress the electron beam and to generate fast density modulations (microbunching) in it. Two spatial harmonics can be present simultaneously in longitudinal density modulations of the electron beam - one with the laser wavelength and the other with half of the laser wavelength, and the ratio of the amplitudes of the harmonics depends on the duration of the laser pulse front. The average density of the electron beam (slow density modulation) can be controlled by changing the form of the laser pulse envelope. The number of microbunches in the compressed electron beam can be changed by varying the amplitude of the laser pulse and the initial length of the electron beam, and for certain conditions, only one electron bunch with an attosecond length can be produced. The results of the theory are compared with 1D PIC (Particle-In-Cell) simulations, and a good agreement is found.open4