2

I don't undertsand why, in Bash:

  • echo $(hostname --fqdn) works,
  • echo $(id -un) doesn't (id: –un: No such user).

Backticks give the same results.

Is there some good practice for using this notation? Does the id command have something special regarding its use in such a way?

Hereunder, some details as it seems it works on other systems:

  • lsb_release -a: SUSE Linux Enterprise Server 11 (x86_64),
  • which id: /usr/bin/id,
  • id -un works alone.
2
  • try to replace $(id -un) with $(/usr/bin/id -un) Mar 13, 2014 at 9:34
  • 5
    ...You wrote a shell script using Word. I humbly suggest you invest in pretty much any actual editor.
    – Jenny D
    Mar 13, 2014 at 13:42

1 Answer 1

5

I noticed a subtle difference between the command posted:

echo $(id -un)

and the error message for it:

id: –un: No such user

The two "hyphens" are different, the second one being a tiny bit longer. It looks like the error message was copied and pasted from the original, but the command was re-typed. The original command most likely contains the wrong kind of hyphen. This explains why no one could reproduce the error message.

This kind of error mostly has one of two reasons:

  • Code was copied from websites, where it was typeset using a bad formatter / syntax highlighter.

  • Code was typed in an inappropriate editing environment, like Microsoft Word. Word might change something you type into a typographical better, but unexpected variant, like different hyphens, different quotes, or some other space sign.


Coincidentally, there was just a popularity contest challenge on Codegolf, where one answer used a similar "trick" to yield a difference that was not even visible: https://codegolf.stackexchange.com/a/23258/16287

6
  • 2
    Code was typed in an inappropriate editing environment, like Microsoft Word. Actually, most Windows-based text editors have this problem too (using the wrong hypen, at least). I've even run into it doing copy-pasta from PowerShell, so it seems to be much more of a Windows-platform thing than any editor in particular. Mar 13, 2014 at 13:34
  • In fact, I have simplified the problem with a advice of one of my coworkers: $(whoami) instead of $(id -un). And also disabled this auto-correction in Word!
    – Doc Davluz
    Mar 13, 2014 at 16:20
  • 1
    @PomCompot Stop using Microsoft Word for shell scripts!!!
    – ewwhite
    Mar 13, 2014 at 18:39
  • At least for programming. :)
    – Dubu
    Mar 13, 2014 at 18:39
  • You fixed the wrong problem!
    – MikeyB
    Mar 13, 2014 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .