Finding 'ls' to bypass system alias

Heyas

Just asking, is there a better to make sure the script will use the binary and not some alias?

LS=$(locate "ls"|grep /bin/ls$ | head -n 1)

Thank you.

'locate' is not a good idea. It's not installed on many systems, and may be unused on some where it is.

Insisting on /bin/ls is not a good idea. Some distributions might insist on putting them somewhere else.

Aliases are an interactive session thing -- they're not generally enabled in scripts at all. You have to go to special effort to make them happen. So this is probably unnecessary.

1 Like
LS='\ls'

Or simply define: LS='/bin/ls' and use $LS in your script - that's what I usually do for most/all the tools.

1 Like

Could you please be a litte bit more elaborate regarding that 'somewher else'?

Figured, if one exports an alias (named 'ls'), it interferes with the use of 'ls' inside a script.
Saying:
In the script i wanted to use ls -l to get a file its bytesize, but the followed math failed as it used the alias, which already was ls -lh .

Thank you

EDIT:
vgersh99, ty i'll try that.

\ls ...

That will bypass any alias for "ls" and use whatever binary it finds in your PATH envval, assuming you're running bash.

1 Like

That isn't just bash . Any POSIX conforming shell will ignore any defined aliases if one or more characters of the command's name are quoted. So, any of the following will work:

"ls" 
\ls
l\s
l's'
3 Likes

When you run a shell script, it shouldn't get your aliases. They're not inherited the way environment variables and the like are.

Shell scripts don't usually process aliases unless they're interactive, either.

How exactly are you running your shell script, for these aliases to get processed? I suppose it'd have your aliases if you were sourcing it.

The POSIX standards don't restrict alias expansions to interactive environments. Any script can define aliases and dot (i.e., source) files that define aliases.

A common example is that many scripts run by cron start with something like:

. $HOME/.profile

to correctly initialize that job's environment and which can define aliases that will affect the execution of that cron job script.