Why do backslashes prevent alias expansion?

后端 未结 3 511
庸人自扰
庸人自扰 2020-12-28 15:53

In the first part of my question I will provide some background info as a service to the community. The second part contains the actual question.

Part I

相关标签:
3条回答
  • 2020-12-28 16:16

    \ls only quotes the first character rather than the whole word. It's equivalent to writing 'l's.

    You can verify it like this:

    $ touch \?l
    $ \??
    bash: ?l: command not found
    

    If \?? quoted the whole word it would say ?? not found rather than ?l not found.

    I.e. it has the same effect as:

    $ '?'?
    bash: ?l: command not found
    

    rather than:

    $ '??'
    bash: ??: command not found
    
    0 讨论(0)
  • 2020-12-28 16:28

    Historically, and maintained by POSIX, quoting any part of the word causes the entire word to be considered quoted for the purposes of functions and alias expansion. It also applies to quoting the end token for a here document:

    cat << \EOF
    this $text is fully quoted
    EOF
    
    0 讨论(0)
  • 2020-12-28 16:29

    Just for completion, here's yet another way to suppress alias & function lookups (by clearing the entire shell environment for a single command):

    # cf. http://bashcurescancer.com/temporarily-clearing-environment-variables.html
    env -i ls
    
    0 讨论(0)
提交回复
热议问题