Which characters are allowed in a bash alias

前端 未结 4 1711
北恋
北恋 2021-02-14 00:05

I recently added

alias ..=\'cd ..\'
alias ...=\'cd ../..\'
alias ....=\'cd ../../..\'

to my bash_aliases file. Playing around with this, I not

相关标签:
4条回答
  • 2021-02-14 00:29

    https://www.gnu.org/software/bash/manual/html_node/Aliases.html I wouldn't name an alias .. in linux . is a special name for the current director and .. is for the parent directory. there could be some obscure process that could be screwed up by aliasing .. for cd

    0 讨论(0)
  • 2021-02-14 00:35

    It's always been safer to use functions instead:

    function .. { cd '..'; }
    function ... { cd '../..'; }
    function .... { cd '../../..'; }
    

    Or for a conservative preference:

    ..() { cd '..'; }
    ...() { cd '../..'; }
    ....() { cd '../../..'; }
    

    What is safer about doing it this way?

    Aliases affect first-class syntax (if, case, etc.) and its commands are specified using quotes. It's like having to use eval unnecessarily. Using functions on the other is straightforward and you know exactly how the arguments are expanded. TL;DR aliases are a hack. It's like using a #define statement in C when you can simply define a function. It's something beginners would love to compose.

    If anyone would want to define default arguments to a command, you can use command to make the function call the external command to avoid recursion. That's the right way to do it. E.g. function grep { command grep --color=auto "$@"; }

    0 讨论(0)
  • 2021-02-14 00:36

    Bash aliases definitely cannot contain these characters: space , tab \t, newline \n, /, $, `, =, |, &, ;, (, ), <, >, ' and ".

    The GNU manual for bash https://www.gnu.org/software/bash/manual/html_node/Aliases.html says:

    The characters /, $, `, = and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.

    "shell metacharacters" are defined in https://www.gnu.org/software/bash/manual/html_node/Definitions.html as

    A character that, when unquoted, separates words. A metacharacter is a space, tab, newline, or one of the following characters: |, &, ;, (, ), <, or >.

    I couldn't find a list of "quoting characters" https://www.gnu.org/software/bash/manual/html_node/Quoting.html. As far as I know they are the single ' and double " quotes.

    0 讨论(0)
  • 2021-02-14 00:40

    According to the following environment variables names can only contain

    uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names.

    But variables are not really the same the same of alias names.

    In practice a -(dash) should be added to the list because it is a defacto standard in debian(apt-get). In addition _~@#%^., all work and ><'"|=()backtick/?[]+!. don't. In addition you have to worry about $PATH and characters like :. It appears space and linebreaks could work if they are properly escaped or quoted like so "a b".

    0 讨论(0)
提交回复
热议问题