Bash – can file expansion work for filenames with a newline character

bash

$ rm Think\ Python:\ How\ to\ Think\ Like\ a\ Computer
\ Scientist\ 2014.pdf 
rm: cannot remove ‘Think Python: How to Think Like a Computer’: No such file or directory
$  Scientist 2014.pdf: command not found
$ rm Think*
$

In the first rm, I use autocompletion in bash to specify a file with a newline character in its filename, and it doesn't work because of the newline character.

In the second rm, I use file expansion to avoid explicitly to specify the new line character. Why can file expansion avoid the problem? Doesn't file expansion expand to the full filename which contains the new line character too?

Best Answer

In the case of rm of the file ending in \n, rm file*, the regexp file expansion "eats" the new line as part of the rm argument, so the shell does not see it and does not parse it as a command.

As seen in a strace:

$strace rm teste.txt* 
execve("/bin/rm", ["rm", "teste.txt\n"],

As for autocompletion obeys to a set of pre-defined rules, and it does not really expect to have a '\n' there.

Related Question