Question

Let's say I have a function in Bash:

function ll {
    command ls -l $*
}

so it is to make ll works like ls -l. Most of the cases, it will work, but

ls -l "ha ha"

can work for the file with name ha ha, but

ll "ha ha"

will fail, because it is taken to be

ls -l ha ha

Is there a way to make this work? I think we could have made it

function ll {
    command ls -l "$@"
}

(note that "$@" is different from "$*", with "$@" meaning individually quoted, while "$*" means all quoted in one string)

But then, ll -t "ha ha" would become ls -l "-t" "ha ha", which actually works, but is kind of weird, and I am not sure if it may break down in some cases.

Is there another way to make it work, and another thing is, I think in the function, command ls -l "$@" and ls -l "$@" is the same? (command is just to run the program directly and not call any possible bash function to prevent recursion from happening?)

Était-ce utile?

La solution

Since the shell performs quote removal before it executes the ls there is no problem. You can safely use

function ll {
    command ls -l "$@"
}

and call it as ll -t "ha ha". See your shell's manual page and search for "quote removal".

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top