Issue
I am pretty confused about below experiment results here and want to understand it better:
-
root@localhost$ TEST=test env | grep test TEST=test -
root@localhost$ TEST=test && echo ${TEST} test root@localhost$ TEST=test; echo ${TEST} test -
root@localhost$ TEST=test && env | grep test root@localhost$ TEST=test; env | grep test -
root@localhost$ TEST=test && TEST=${TEST} env | grep test TEST=test root@localhost$ TEST=test; TEST=${TEST} env | grep test TEST=test -
root@localhost$ export TEST=test && TEST=${TEST} env | grep test; unset TEST TEST=test root@localhost$ export TEST=test; TEST=${TEST} env | grep test; unset TEST TEST=test
My main confusion being that:
Why does not #3 work?
Why does #4 work?
Solution
There are two types of vars: Environment (exported) vars, and shell vars.
VAR=valsets the env var namedVARif it already exists.VAR=valsets the shell var namedVARif env varVARdoesn't exist.VAR=val cmdsets the env var namedVARforcmdonly.
The env vars are provided to child processes as their environment.
Shell vars, on the other hand, exist only within that shell process. Child processes have no access to them.
So,
- You set env var
TESTfor the command. - You set shell var
TEST. - You set shell var
TEST. - You set shell var
TEST. Then you set env varTESTfor the command. - You env shell var
TEST. Then you set env varTESTfor the command.
Why does #4 work?
You set an env var.
Why does not #3 work?
You did not set any env vars.
Answered By - ikegami Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.