3 ms·
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 vari
by 1vuio0pswjnm7 3y ago
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 variables 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.
Using djb's envdir utility, one can put a selection of different HTTP headers into a directory so that there's no need to type them on the command line.
cd /path/to/dir
echo close > connection
echo aplication/x-www-form-urlencoded > content-type
echo id=123\;key=xyz > cookie
echo 19 > content-length
echo POST > httpMethod
echo /api/whatever > path
echo "{ \"key\": \"value\" }" > post-data
cd
echo https://example.com \
|if cd /path/to/dir;envdir . yy025;then
cat post-data;fi \
|nc -vvn 127.127 80
It's strange to me 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.
From the article
$ export FRED=value
$ printenv | fgrep FRED
FRED=value
$ FRED=
$ printenv | fgrep FRED
FRED=
$ unset FRED
$ printenv | fgrep FRED
$ # ie, no output from printenv
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
- zerocrates 3y agoA 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.
- Izkata 3y ago"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.
- 1vuio0pswjnm7 3y agoGood 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.
- deleted 3y ago[deleted]
- siebenmann 3y agoIn 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.)
- 1vuio0pswjnm7 3y agoMissing 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' echo 'FRED=barney set|grep ^FRED'|sh
- _TwoFinger 3y ago> Missing semicolon. It's not missing. PP is making a point about "single-command variables."
- Izkata 3y agoSingle-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)
- dcassett 3y ago>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
- deleted 3y ago[deleted]
- 1vuio0pswjnm7 3y agoWithout 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.) Another thing is that printenv does not make it easy to see spaces or other nonprinting characters in the values of environmental variables. For example PS="# " displays as PS=#. TAB=$(printf '\11') displays as TAB=. For me, that's not really helpful. Whereas the builtin set command displays the values in single quotes so one can easily the presence of spaces and other non-printing characters. A workaround is to use printenv with sed, something like printenv|sed -n l If I was going to investigate "single command environmental variables" I might just use getenv and printf. That way I can see the non-printable characters. But not being an IT person or a programmer, maybe I am missing the reason why printenv, written in 1979, would be preferable to getenv. (That, IMO, would be an interesting blog post.) For example, int printf(const char *__restrict, ...); char *getenv(const char *); int main() { if(getenv("FRED")) printf("%s=%s\n","FRED",getenv("FRED")); return 0; } usage: FRED= a.out