1

Just wanted to know - is there a standard path where uname is installed? e.g. /usr/bin/uname or /bin/uname

I'm asking with regards to HP-UX, AIX, SunOS and RedHat Linux

Is there a common path in which all of the above contain uname? If not, how can I know on which OS my script is executing?

RonK
  • 241
  • 1
  • 5
  • 13

3 Answers3

1

uname should be in a location that's in your $PATH which almost always includes /bin and /usr/bin. You should be able to rely on executing uname without specifying a full path.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • Doesn't it matter which "copy" of uname i get? – RonK Aug 25 '10 at 19:12
  • @RonK: Do any of your systems have `uname` in more than one place? The designers of some versions of Linux or Unix choose to put some utilities in different locations. Sometimes utilities appear in more than one location on the same system. If this is the case, they are often symlinked or hardlinked and so it makes no difference. Occasionally, utilities will be located in more than one place that are different versions. At least one of those will not be in either `/bin` or `/usr/bin`. If they're different and one is in each of those locations it's a mistake or bad design. – Dennis Williamson Aug 25 '10 at 19:38
  • `uname` is a basic utility so I don't think you should worry about it at all. – Dennis Williamson Aug 25 '10 at 19:38
  • @Dennis Williamson: What if my script is the one responsible for setting the PATH? Can I assume /bin or /usr/bin is are already in the path even for a brand new account? (I am no SysAdmin so forgive me if the question sounds stupid) – RonK Aug 25 '10 at 20:17
  • @RonK: Try this at a command prompt: `env -i bash -c 'echo $PATH'`. That will show you what the default `PATH` is. The shell creates that value by default. If your script sets a `PATH` this way: `PATH=$PATH:/more/dirs` then the exported value will be there along with your new value. If you do it this way: `PATH=/some/dir` then the old value will be replaced instead of appended to. What do you mean "brand new account" - this is the first you've mentioned that. If you're talking about adding new users (or service accounts) then yes, they get a default environment... – Dennis Williamson Aug 25 '10 at 21:32
  • ...almost always including `/bin` and `/usr/bin` in the `PATH`. Those two locations are very basic and it's safe to assume that normally they will be there. – Dennis Williamson Aug 25 '10 at 21:33
  • @Dennis Williamson: Thanks, your last comments were the answer I was hoping to get. B.T.W., I ran the command on Redhat and got '/usr/local/bin:/bin:/usr/bin' So it looks good. – RonK Aug 26 '10 at 03:28
  • It’s vital to use an absolute path to every external command in most scripts because using a relative name or `env` causes vulnerabilities and compatibility issues, unless the script overwrites `PATH` with a sane, system-compatible default. –  Jun 12 '18 at 20:04
0

If you are making a general purpose script, you can include /usr/bin and /bin in the pathname if you are unsure. Then it would not matter.

mdpc
  • 11,856
  • 28
  • 53
  • 67
0

You can never rely on binaries being in a particular location across all operating systems. Generally, for portability's sake, you will want to rely on the path being set properly. But safe programs tends to lock down the path (and Perl's taint mode modifies the path too). Thus perhaps offer the user of the script the ability to configure the binaries to be used.

PP.
  • 3,316
  • 6
  • 27
  • 31