trailing question mark in filename

I have a script(ex.sh) with one line in it, running in bash shell.

ls -l > /usr/ngasi/contexts/tdevoe/private/ex.txt

when I run it , it creates the file with a trailing question mark

-rwx------ 1 tdevoe webapp 59 Jun 7 06:42 ex.sh
-rw------- 1 tdevoe webapp 3761 Jun 7 06:42 ex.txt?

why?

I think you've got gremlins.

"Gremlin" is a colorful term for strange, nonprinting characters that your text editor might not show you but can really foul things up anyway. That's not a literal question mark, it's ls thinking "What the hell is that? I can't print that!" so it just prints a question mark instead. Gremlin characters are even more annoying in C files, the compiler just dies.

I often get lots of gremlins from copy/pasting stuff from the internet. If that's how you ended up with that one, there's probably lots more.

You can get rid of gremlins like:

tr -d '[\000-\010\014\016-\037\177]' < messed-up.sh > fixed.sh

This will strip out any characters but ASCII printing characters, newline, carriage return, tab, and space.

P.S. Don't feed them

A lot of editors have a hex display mode. I use Ultra Edit for finding embedded junk characters, and dos2ux to clean up files brought over from Windows systems.

You could try "ls -b" to display any gremlin characters in octal notation (\nnn)