-3

From coreutils manual, about stat command

Due to shell aliases and built-in stat functions, using an unadorned stat interactively or in a script may get you different functionality than that described here. Invoke it via env (i.e., env stat ...) to avoid interference from the shell.

  1. What does "shell aliases and built-in stat functions" mean? In bash, I didn't find an alias or builtin in or function named stat

    $ whereis stat
    stat: /usr/bin/stat /usr/bin/X11/stat /usr/share/man/man1/stat.1.gz /usr/share/man/man2/stat.2.gz
    $ help stat
    bash: help: no help topics match `stat'.  Try `help help' or `man -k stat' or `info stat'.
    
  2. Why can "Invoke it via env (i.e., env stat ...)" "avoid interference from the shell"?

    Can't a command following env be an alias, built-in command, or function?

    Is putting env in front of a command a way to specify that the command is an external executable?

3
  • IMO, using an external binary in a script without giving its explicit path is potentially-dangerous anyway, but of course, then you decide between /bin/stat and /usr/bin/stat. (I wouldn't have stat high on my list of commands for which this happens either, unlike ls and rm, but it's better to have more boilerplate than too little.) Dec 30, 2016 at 8:32
  • Oh, and env alone won't unset LANG which frankly is as much of a danger if you parse command output as anything. Dec 30, 2016 at 8:34
  • 2
    Why is this getting downvoted so much?
    – phemmer
    Dec 30, 2016 at 13:38

1 Answer 1

5
  1. Bash may not have a stat built-in, but other shells may. In fact, a shell may provide built-in versions of any utility it wants to, and a user may override stat with their own shell function or alias calling another binary.

  2. No. The utility that env executes can not be a built-in (try it with cd for example). Aliases and shell functions are similarly not allowed.

The manual is just trying to make sure that the user is getting the described functionality. In my opinion, if it wants to be really paranoid, it should also mention that you ideally should call stat with its absolute path. This makes more sense on non-Linux systems where the GNU coreutils are an optional extra (often installed in /usr/local/gnu/bin or somesuch place, or with a g prefix, e.g. gstat, gls, gmv etc.)

2
  • Thanks.Is putting env in front of a command a way to specify that the command is an external executable?
    – Tim
    Dec 30, 2016 at 12:15
  • @Tim Yes, in a way. The env utility will only execute utilities external to the shell (and fail otherwise). It also allows you to execute a program with specific environment variables set, e.g. env CCHACHE_DIR="$HOME/.ccache" ccache -s. It's more often used for this than making sure that a particular command is an external utility.
    – Kusalananda
    Dec 30, 2016 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.