First, please use ***
[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do
not use quote tags, bolding, colors, or other fancy formatting.
Second, this...
Code:
alias cl="xargs -I {} bash -c 'cd {} && ls -alh' <<<"
...doesn't work because it spawns a new shell instance. The environment changes made there (i.e. pwd) are lost when that shell exits, so you end up back where you started.
Also, remember that an alias is just a text substitution feature, not a command. If the first token (word) on the line matches a preset alias, the text of the alias is inserted at that point. Everything after the first token stays where it is. So there's no way to position arbitrary arguments that way. That's what makes shell functions so much more flexible. They're basically scripts that execute inside the current environment.
As for your second one, first, understand that "
[" is a command (an synonym for
test), and everything after it is an argument to that command, so they need to be whitespace separated. Also,
test doesn't have pattern-matching ability.
As long as you're using
bash, you should use the new "
[[" keyword instead, which is broadly compatible, but more flexible, and does support pattern matching.
Speaking of which, I'm not really sure what you are doing here:
This appears to be trying to match a single, two letter string. Is that right? But not only is it the old
test, it's also quoted, which disables the globbing power of "?". Or perhaps this is some attempt to carry over syntax from csh.
For pattern matching two characters (of any kind) in bash, you'd use this:
But I'm not sure this is what you really want anyway. I think you just want to know if a
$2 entry exists. So just use this:
I suggest also putting in some sanity code too, such as a test to see if the directory exists before changing to it.
See the following links for more on bash tests. When using
bash or
ksh, it's recommended to use
[[..]] for string/file tests, and
((..)) for numerical tests. Avoid using the old
[..] test unless you specifically need POSIX-style portability.
http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr