I’m implementing builtins env and export in a project recreating bash shell. I have inside my shell copy of envp, t_env **envp
Then I modify it.
What is the conceptual difference between
export a
and export a=
?
Why in first case I can see it with command export
and not env
?
How to implement this behavior?
At the moment my implementation is to use flag, to distinguish between env and shell variables, but maybe there is a better way?
And why even exists this option, set variable without value?
9
This exists for historical reasons.
I believe in the original Bourne Shell, you could not assign the variable at the same time as exporting it. So you had to do it in two steps:
variable=value
export variable
Later on these were combined, so you could write
export variable=value
but the original export
without assignment is still allowed for compatibility
1
export a
makes it possible for a
to be added to the environment of a child process but nothing is actually added unless the variable has been assigned a (possibly null) value.
testenv(){
bash -c '
declare -p myvar0 myvar1 myvar2 myvar3
unset_or_null(){
declare -n var=$1
case ${var-1}/${var:-1}/${var:+1} in
1/*/*) echo $1 is unset ;;
*/1/*) echo $1 is null ;;
*/*/1) echo $1 is set ;;
esac
}
unset_or_null myvar0
unset_or_null myvar1
unset_or_null myvar2
unset_or_null myvar3
'
}
export myvar1
export myvar2=
export myvar3=ok
testenv
echo -----
myvar0=
myvar1=
testenv
echo -----
myvar0=ok
myvar1=ok
testenv
bash: line 2: declare: myvar0: not found
bash: line 2: declare: myvar1: not found
declare -x myvar2=""
declare -x myvar3="ok"
myvar0 is unset
myvar1 is unset
myvar2 is null
myvar3 is set
-----
bash: line 2: declare: myvar0: not found
declare -x myvar1=""
declare -x myvar2=""
declare -x myvar3="ok"
myvar0 is unset
myvar1 is null
myvar2 is null
myvar3 is set
-----
bash: line 2: declare: myvar0: not found
declare -x myvar1="ok"
declare -x myvar2=""
declare -x myvar3="ok"
myvar0 is unset
myvar1 is set
myvar2 is null
myvar3 is set