Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the ls version

Tags:

shell

This topic is about the util 'ls' The BSD version uses the parameter '-G' to color up the output, while the Linux version uses parameter '--color'

Also the environment variable to set the colors is different: BSD: $LSCOLORS Linux: $LS_COLORS

But now the problem is: I want to determine which version is installed (using a small Shell script), so I can set alias ls and the environment appropriate in my .bachrc file.

like image 488
To1ne Avatar asked Dec 17 '22 05:12

To1ne


2 Answers

As I mentioned above this seems to me to be the handiest method

if ls --color -d . >/dev/null 2>&1; then
    GNU_LS=1
elif ls -G -d . >/dev/null 2>&1; then
    BSD_LS=1
else
    SOLARIS_LS=1
fi

I've essentially this in my l script, which I use on various platforms to tweak ls output as I like

like image 69
pixelbeat Avatar answered Jan 19 '23 03:01

pixelbeat


Just run 'ls' and see whether it throws an error, e.g. on my mac:

$ ls --color 1>/dev/null 2>&1
$ echo $?
1

Whereas

$ ls -G 1>/dev/null 2>&1
$ echo $?
0

Indicating -G is supported, but --color is not.

like image 32
martin clayton Avatar answered Jan 19 '23 02:01

martin clayton