1

When it comes to the shell operation, I can understand the step one, but the second step a little puzzles me. Here is my understanding:

First, shell splits input into words and operators, then performs according to quoting rules. Here is some of my questions. When I execute:

$ \n
bash: n: command not found

Why is it the n: command not found? not \n: command not found?

$ $'\n'
bash: $'\n': command not found

Why doesn't it perform ANSI-C Quoting?

By the way, when the input is a script file, how does it split into words and operators? Line by line or as a whole?

1 Answer 1

2

Backslash quotes the following character (see QUOTING in man bash). \n is therefore interpreted as n (and being the first word of a command, this would prevent alias expansion, see ALIASES in man bash).

$'\n' is interpreted as ANSI-C quoted. Bash just uses the same quoting to show the problematic character. You can get the same error from

'
'

which is equivalent to it.

The process of word splitting is described at the beginning of man bash. If you're in doubt, ask a more concrete question, or check the source code.

3
  • @choraba, is the meaning of escape here something different than that in C/C++ string, is \<char> just <char> when it comes to bash escaping? where <char> could be any single character. But why this would preven alias expansion? man bash just says Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. Isn't command \n just the case? Sorry for the delay. :)
    – Alaneuler
    Apr 1, 2017 at 1:21
  • Yes, outside of quotes, \n and n are the same. Try it with l\s. Backslash prevents alias expansion by design. \n is the same as n, both return command not found.
    – choroba
    Apr 1, 2017 at 19:32
  • Thank you! I think I fully understand it now. And the wiki page about escape helps me understand better.
    – Alaneuler
    Apr 2, 2017 at 12:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .