linuxshellenvironment-variableszshunset

Why both the set and unset commands don't work as expected in my macOS terminal


I'm very confused by this result.

I have two questions.

  1. What's wrong with that?

  2. What are *, @, and argv?

$ set GITHUB_ENV_var=1
$ echo $GITHUB_ENV_var

# ❌ after set, echo nothing
$ set | grep GITHUB_ENV_var
'*'=( 'GITHUB_ENV_var=1' )
@=( 'GITHUB_ENV_var=1' )
argv=( 'GITHUB_ENV_var=1' )

$ unset GITHUB_ENV_var
# ❌ unset no work at all
$ set | grep GITHUB_ENV_var
'*'=( 'GITHUB_ENV_var=1' )
@=( 'GITHUB_ENV_var=1' )
argv=( 'GITHUB_ENV_var=1' )

I'm using the zsh as the default shell.

enter image description here

expect result

$ set GITHUB_ENV_var=1
$ echo $GITHUB_ENV_var
1

update

I know that I can use export GITHUB_ENV_var=1 to set a system-wide variable value, but I wonder why the set and unset commands did not work as expected.


Solution

  • I'm really sorry for asking this question without fully understanding the "set" command.

    I understand three things after reading the book Linux Command Line and Shell Scripting Bible, 3E

    1. The set command is not designed for setting environment variables;
    2. The export command is used to set system-wide environment variables;
    3. When I learned that the unset command can delete environment variables, I mistakenly thought that the set command and the unset command were the opposite functions;
    $ man zshbuiltins
    
    
           set [ {+|-}options | {+|-}o [ option_name ] ] ... [ {+|-}A [ name ] ]
               [ arg ... ]
                  Set the options for the shell and/or set the positional parameters, or
                  declare and set an array.  If the -s option is given, it causes the
                  specified arguments to be sorted before assigning them to the positional
                  parameters (or to the array name if -A is used).  With +s sort arguments
                  in descending order.  For the meaning of the other flags, see
                  zshoptions(1).  Flags may be specified by name using the -o option. If no
                  option name is supplied with -o, the current option states are printed:
                  see the description of setopt below for more information on the format.
                  With +o they are printed in a form that can be used as input to the shell.
    
                  If the -A flag is specified, name is set to an array containing the given
                  args; if no name is specified, all arrays are printed together with their
                  values.
    
                  If +A is used and name is an array, the given arguments will replace the
                  initial elements of that array; if no name is specified, all arrays are
                  printed without their values.
    
                  The behaviour of arguments after -A name or +A name depends on whether the
                  option KSH_ARRAYS is set.  If it is not set, all arguments following name
                  are treated as values for the array, regardless of their form.  If the
                  option is set, normal option processing continues at that point; only
                  regular arguments are treated as values for the array.  This means that
    
                         set -A array -x -- foo
    
                  sets array to `-x -- foo' if KSH_ARRAYS is not set, but sets the array to
                  foo and turns on the option `-x' if it is set.
    
                  If the -A flag is not present, but there are arguments beyond the options,
                  the positional parameters are set.  If the option list (if any) is
                  terminated by `--', and there are no further arguments, the positional
                  parameters will be unset.
    
                  If no arguments and no `--' are given, then the names and values of all
                  parameters are printed on the standard output.  If the only argument is
                  `+', the names of all parameters are printed.
    
                  For historical reasons, `set -' is treated as `set +xv' and `set - args'
                  as `set +xv -- args' when in any other emulation mode than zsh's native
                  mode.