Discussion:
[Help-bash] echo and environment variables
Christof Warlich
2017-10-05 17:22:34 UTC
Permalink
Hi,

I'm a bit surprised: Can anyone tell why

$ xxx=hiho echo $xxx # xxx is only set for the current command

prints an empty line, while

$ xxx=hiho; echo $xxx; unset xxx # please note the semicolon!

prints

hiho

as expected?

Furthermore, note that these few lines of C code:

#include <stdio.h>
#include <stdlib.h>
int main () {
  char *xxx = getenv ("xxx");
  if(xxx != NULL) printf("%s\n", xxx);
  return 0;
}

_do_ print

hiho

when called either way, i.e like this.:

$ xxx=hiho ./a.out
hiho

and like this:

xxx=hiho; ./a.out; unset xxx
hiho

Thanks for any ideas,

Chris
Pierre Gaston
2017-10-05 17:33:29 UTC
Permalink
Post by Christof Warlich
Hi,
I'm a bit surprised: Can anyone tell why
$ xxx=hiho echo $xxx # xxx is only set for the current command
prints an empty line, while
$ xxx=hiho; echo $xxx; unset xxx # please note the semicolon!
prints
hiho
as expected?
see http://mywiki.wooledge.org/BashFAQ/104
Nick Chambers
2017-10-05 17:28:01 UTC
Permalink
Post by Christof Warlich
Hi,
I'm a bit surprised: Can anyone tell why
$ xxx=hiho echo $xxx # xxx is only set for the current command
prints an empty line, while
$ xxx=hiho; echo $xxx; unset xxx # please note the semicolon!
prints
hiho
as expected?
#include <stdio.h>
#include <stdlib.h>
int main () {
char *xxx = getenv ("xxx");
if(xxx != NULL) printf("%s\n", xxx);
return 0;
}
_do_ print
hiho
$ xxx=hiho ./a.out
hiho
xxx=hiho; ./a.out; unset xxx
hiho
Thanks for any ideas,
Chris
http://mywiki.wooledge.org/BashFAQ?action=show&redirect=BashFaq#BashFAQ.2F104.Why_doesn.27t_foo.3Dbar_echo_.22.24foo.22_print_bar.3F
Loading...