#Long reads
bedtools bamtofastq -i input.bam -fq output.fastq
#For paired-end reads:
samtools sort -n input.bam -o input_sorted.bam # sort reads by identifier-name (-n)
bedtools bamtofastq -i input_sorted.bam -fq output_r1.fastq -fq2 output_r2.fastq