当前位置: 动力学知识库 > 问答 > 编程问答 >

unix - Why doesn't this bash function work?

问题描述:

I want to make a shortcut function for ls -rt, but the -rt is not accepted by Unix in my definition. Why didn't this work? What is the solution?

lsr () {ls -rt $1;}

网友答案:

Your particular problem is that you need white space after the opening brace, such as (quoting properly and using all arguments as well)

lsr() { ls -rt "[email protected]"; }

But you may also want to consider an alias since this is substitution at the start of the command:

alias lsr='ls -rt'

This is, after all, how many systems give you the very handy "long ls", with something like:

alias ll='ls -al'

You'll just need to make sure this is present in something that's called for specific cases as needed, such as in .bashrc for interactive shells, or in a file sourced at the start of your script itself (and ensuring aliases are expanded with shopt -s expand_aliases). Those are two possibilities but there are no doubt others.

网友答案:

add whitespace after {

lsr () { ls -rt $1;}

网友答案:

Could it be the missing space after the first "{" ?

# bash --version
GNU bash, version 4.3.42(1)-release (i686-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
#
# touch a
# touch b
# touch c
# ls -rt
a  b  c
# ls -t
c  b  a
#
# lsr() {ls -rt;}
bash: syntax error near unexpected token `{ls'
# lsr() { ls -rt;}
# lsr
a  b  c
网友答案:

As has been noted, the { must be followed by whitespace for the function definition to work.

shellcheck.net is a great site for syntax-checking shell code, which would have caught the problem:

lsr () {ls -rt $1;}
        ^-- SC1054: You need a space after the '{'.
               ^-- SC2086: Double quote to prevent globbing and word splitting.

As you can see, it not only found the problem, but also provided a helpful suggestion to double-quote $1, so as to make the argument work with filenames with embedded spaces and glob characters too.

As for why the whitespace after { is needed:

{ is an instance of a so-called shell keyword, as opposed to the so-called shell metacharacters | & ; ( ) < > space tab.

Metacharacters separate words during parsing, keywords do not.

In the case at hand, the { in {ls is not recognized as a shell keyword, because it isn't recognized as a separate word due to immediately being followed by ls.

Bash therefore sees single word {ls, which is unexpected at this position, and fittingly reports syntax error near unexpected token '{ls'.

Note that the closing } does not need preceding whitespace (even though you may want to add it for visual symmetry), because it is preceded by metacharacter ;, which is by definition recognized as its "own thing", and thus implicitly causes the } to be recognized as its own word.

On a related note: Since the closing } is on the same line as the ls command, the ls command must be terminated with control operator ; - otherwise, the } would be interpreted as another argument to ls.

分享给朋友:
您可能感兴趣的文章:
随机阅读: