Skip to main content
added 208 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a "special built-in", and so according to POSIX, the result of the assignment in VAR=hello : should be visiblepersist in the shell environment after : returns. Bash doesn't do that in normal mode though, but does in POSIX mode, as does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode bash (bash called with -o posix or --posix or with sh or anything/sh as its argv[0] or with POSIXLY_CORRECT or SHELLOPTS=posix in its environment), dash, oryash, ksh or zsh in sh or ksh emulation. That would be different with e.g. true instead of :, since it's just a regular command (builtin or not).

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a "special built-in", and so according to POSIX, the assignment in VAR=hello : should be visible in the shell environment. Bash doesn't do that in normal mode though, but does in POSIX mode, as does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode bash (bash -o posix), dash, or ksh. That would be different with e.g. true instead of :, since it's just a regular command (builtin or not).

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a "special built-in", and so according to POSIX, the result of the assignment in VAR=hello : should persist in the shell environment after : returns. Bash doesn't do that in normal mode though, but does in POSIX mode, as does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode bash (bash called with -o posix or --posix or with sh or anything/sh as its argv[0] or with POSIXLY_CORRECT or SHELLOPTS=posix in its environment), dash, yash, ksh or zsh in sh or ksh emulation. That would be different with e.g. true instead of :, since it's just a regular command (builtin or not).

being a _special_ built-in is exactly what makes : follow different rules here. Obscure? Oh yes. But the example was with :, and that's what the standard says and what other sh-like shells do with : ...
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a shell builtin"special built-in", and so according to POSIX, the assignment in VAR=hello : should be visible in the shellshell environment. Bash doesn't do that in normal mode though, but does in POSIX mode, as does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode bash (bash -o posix), dash, or ksh. That would be different with e.g. true instead of :, since it's just a regular command (builtin or not).

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a shell builtin, and so according to POSIX, the assignment in VAR=hello : should be visible in the shell environment. Bash doesn't do that in normal mode though, but does in POSIX mode, as does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode bash (bash -o posix), dash, or ksh. That would be different with e.g. true instead of :, since it's just a regular command.

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a "special built-in", and so according to POSIX, the assignment in VAR=hello : should be visible in the shell environment. Bash doesn't do that in normal mode though, but does in POSIX mode, as does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode bash (bash -o posix), dash, or ksh. That would be different with e.g. true instead of :, since it's just a regular command (builtin or not).

added 81 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a special built-inshell builtin, and so according to POSIX, the assignment in VAR=hello : should per POSIX be visible in the following shell shell environment. Bash doesn't do that in normal mode though, but does in POSIX modePOSIX mode, and soas does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode Bashbash (bash -o posix), Dashdash, or ksh. That would be different with e.g. true instead of :, since it's just a regular command.

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a special built-in, and so the assignment in VAR=hello : should per POSIX be visible in the following shell environment. Bash doesn't do that in normal mode though, but does in POSIX mode, and so does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode Bash (bash -o posix), Dash, or ksh. That would be different with e.g. true instead of :, since it's just a regular command.

It seems that you are basically asking about exporting and when something needs to be exported. Let's look at each of your examples:

  1. VAR=hello; echo $VAR: these are two commands, run in the same shell session. Since we are in the same session, the first variable assignment is enough and there is no need to export anything.

  2. VAR=hello :; echo $VAR: here too we have two commands in the same shell session but in this case, the variable was explicitly only set for the duration of the : command. So here, you didn't actually set the variable in your shell session, you only set it for that one command. So when you get back to the shell, the variable is unset hence the echo prints an empty line.

  3. VAR=hello; echo $VAR; bash -c 'echo $VAR': here we have one shell session in which you run VAR=hello and echo $VAR. You then start a new shell session, which will therefore not inherit unexported variables, and in that second session, the echo $VAR prints nothing. This is where exporting would have made a difference:

    $ var=hello; bash -c 'echo $var'
    
    $ export var=hello; bash -c 'echo $var'
    hello
    

Though as was pointed out in comments, : is a shell builtin, and so according to POSIX, the assignment in VAR=hello : should be visible in the shell environment. Bash doesn't do that in normal mode though, but does in POSIX mode, as does pretty much any other POSIX shell.

So unset VAR; VAR=hello :; echo "$VAR" does print hello in POSIX-mode bash (bash -o posix), dash, or ksh. That would be different with e.g. true instead of :, since it's just a regular command.

I suppose we can just integrate the comment in?
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
Loading
added 627 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading
Post Undeleted by terdon
deleted 201 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading
Post Deleted by terdon
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading