I want to shorten this to a one liner:
if ls --version 2>/dev/null | grep -q 'coreutils'; then alias ls='ls --color=always'
else alias ls='ls -G'
fi
I frequently use []
for conditionals, e.g. [ -z(or -n) condition]
but haven't had to do an else part in this format yet.
In ruby I would do condition ? true : false
Is there an equivalent in bash?
I tried
[ -z ls --version 2>/dev/null | grep -q 'coreutils'] ? alias ls='ls --color=always' : alias ls='ls -G'
but obviously that's not the right way to do it.
I want a one-liner because I want all my .bashrc
to fit in a smallish window (< ~50 lines) so, as with other multi-liners I condense them into one liners when possible. I am comfortable with sacrificing readability for size for this example.
I also tried:
[ -z ls --version 2>/dev/null | grep -q 'coreutils'] && alias ls='ls --color=always' || alias ls='ls -G'
and
[ -n ls --version 2>/dev/null | grep -q 'coreutils'] && alias ls='ls --color=always' || alias ls='ls -G'
and
[ ls --version 2>/dev/null | grep -q 'coreutils'] && alias ls='ls --color=always' || alias ls='ls -G'
but I always get the 'else' alias not the 'then' alias
I could just reverse it but I suspect that as it's not working right that wouldn't help. Only way to be sure though is to try it on OSX and I'm on ubuntu right now.
EXPR1
will always return true as otherwiseEXPR2
will also be executed. – Stéphane Chazelas May 10 '14 at 15:10