bash script prepending ? to file name

I am using the below script. When I have it echo $f as shown below, it gives the correct result:


while read p; do
    f=$(echo $p | sed "s/${var}/\\n/g")
    echo $f
    ~/bin/samtools view $p | awk '{print "@"$1"\n"$10"\n+\n"$11}' > $f
    ./ $f ${f%.fastq}
    rm ${f%.sliced.bam}.fastq
done < $1

I get the output as expected


But the file being created by awk > $f has the name


Note that the overall goal here is to run this loop on every file listed in a file with absolute paths but then write locally (which is what the sed call is for)

edit: Run directly on the command line (without variables) the samtools | awk line runs correctly.


Awk cannot possibly have anything to do with your problem. The shell is completely responsible for file redirection, so f MUST have a weird character in it.

Most likely whatever you are sending to this script has a special character in it (e.g. perhaps a UTF character, and your terminal is showing ASCII only). When you do the echo, the shell doesn't know how to display the char, and probably just shows it as whitespace, and when you send it through ls (which might be doing things like colorization) it combines in a strange way and ends up showing the ?.

Oh wait...why are you putting a newline into the filename with sed??? That is possibly your problem...try just:

sed "s/${var}//g"

Need Your Help

How do I use PORT ftp raw command in c#?

c# ftp port

I am trying to make a FTP client in c#. I found a class that support basic FTP commands on

options menu not inflating correctly in page change in view pager

android android-fragments android-viewpager oncreateoptionsmenu

I have a view pager with each fragment having its own options menu which is inflated when the fragment comes is current one in the view pager however, it is not working as expected. Sometimes the c...