News for hackers. Stuff that matters.
Re:The roots of an obscure Bourne shell error message by 1vuio0pswjnm7 on Monday September 11, 2023 @08:57PM (#37475715)
This does not seem obscure to me at all. Having learned about C from reading djb's programs, I write programs that read their arguments from environmental variables. (See djbdns for example.) When I run one of these programs it's common to set the variable on the command line, e.g.,

   echo https://example.com|Connection=keep-alive yy025|nc -vvn 127.127 80
yy025 sets the HTTP Connection header according to the value of the corresponding environmental variable. If I want to send a POST request I set the variable httpMethod to POST. The address 127.0.0.127 is a TLS forward proxy. Original netcat allows one to type 127.127 without the zeros.

It's strange to see this author use printenv instead of set (a built-in) to display Bourne shell environmental variables. As someone who uses Bourne shell every day and on a variety of computers, I never use printenv. On the smaller systems I make, I do not include printenv in the userland.

The use of fgrep to match a single word in all caps is also strange.

Maybe I am just missing some UNIX wisdom here. If so, please disregard this comment.

   set|grep FRED
Re:The roots of an obscure Bourne shell error message by zerocrates on Monday September 11, 2023 @09:21PM (#37475899)
A lot of people are probably just not familiar with the single-command envvar syntax, and of people that do know it some of them probably don't know the syntax for unsetting.

So it's just not immediately obvious to many why a simple extra space is a problem at all, and why it's this specific error. It can also just generally be hard to see past what you intended to write to what you actually did and how that will be parsed.

Re:The roots of an obscure Bourne shell error message by Izkata on Monday September 11, 2023 @09:43PM (#37476088)
"printenv" (or just "env", the one I habitually use) shows only environment variables, not all available functions and variables like "set" does.

But otherwise yeah, I also don't see this as obscure at all. Was expecting something confusing when, say, fiddling with arrays or something.

Re:The roots of an obscure Bourne shell error message by 1vuio0pswjnm7 on Monday September 11, 2023 @09:53PM (#37476163)
Good point about functions. Normally it's not something I notice because I'm not using Bash. But every once a while I use set in Bash and I'm blasted with a slew of preloaded functions.
Re:The roots of an obscure Bourne shell error message by siebenmann on Monday September 11, 2023 @10:34PM (#37476429)
In this case, one reason to use printenv is that it is an external command, and so it is clearly and unambiguously seeing (and reporting) whatever the Bourne shell would export into the environment for a real command. In this specific case, it appears that set does not report such single-command variables (whether or not they have a value, eg 'FRED=barney set | grep FRED').

(I am the author of the linked-to entry.)

Re:The roots of an obscure Bourne shell error message by 1vuio0pswjnm7 on Monday September 11, 2023 @10:57PM (#37476553)
Missing semicolon.

   FRED=barney; set | grep FRED
Also there is no need for grep, nevermind grep -F (fgrep), if using printenv.

   FRED=barney printenv FRED
Without semicolon

   sh -c 'FRED=barney set'|grep ^FRED
Re:The roots of an obscure Bourne shell error message by dcassett on Monday September 11, 2023 @11:15PM (#37476632)
>Missing semicolon.

If you add a semicolon then FRED is no longer a single command variable (it persists)

> on NetBSD, one can do this

Linux also does the expected thing:

$ FRED=barney printenv FRED

barney

Re:The roots of an obscure Bourne shell error message by _TwoFinger on Monday September 11, 2023 @11:11PM (#37476615)
> Missing semicolon.

It's not missing. PP is making a point about "single-command variables."

Re:The roots of an obscure Bourne shell error message by Izkata on Tuesday September 12, 2023 @06:17PM (#37489332)
Single-command environment variables, to be precise. The same thing that gets created when you use "export", but isn't created when you use "=" on a line on its own:

  $ FOO=BAR env | grep FOO   # Exists as an environment variable passed to env
  FOO=BAR
  $ env | grep FOO           # But only for that command
  $ FOO=BAR                  # Creates a variable (not an environment variable)
  $ env | grep FOO           # As shown by not being passed down
  $ echo $FOO                # But still accessible in this context
  BAR
  $ export FOO=BAR           # Promotes it to an environment variable (the "=BAR" is optional since it's already defined)
  $ env | grep FOO           # And as such is now visibly passed down
  FOO=BAR
(Yes, bash does mix these together so it's easy to accidentally replace something you don't intend to)
Re:The roots of an obscure Bourne shell error message by 1vuio0pswjnm7 on Tuesday September 12, 2023 @05:08PM (#37488382)

   Without using printenv or grep -F (in shell script /bin/fgrep)

   $ export FRED=value
   $ set|grep FRED
   FRED='value'
   $ FRED=
   $ set|grep FRED
   FRED=''
   $ unset FRED
   $ set|grep FRED
   $ # ie, no output from set
(Missed a dollar sign.)