Stack Exchange
How to get the correct value of keys in \pgfdeclareshape
Here is a piece of test code. The output in the log file is
outside test: initial
(../output/test.aux) ABD: EveryShipout initializing macros
Run number 1 of rule 'xelatex'
savedanchor test: changed
saveddimen test: changed
anchor test: initial

\documentclass[tikz]{standalone}
\pgfkeys{
  test/.store in=\test,
  test=initial,
}
\pgfdeclareshape{myshape}{
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \savedanchor{\anyanchor}{
    \typeout{savedanchor test: \test}
  }
  \saveddimen{\anydimen}{
    \typeout{saveddimen test: \test}
  }
  \typeout{outside test: \test}
  \anchor{any}{
    \typeout{anchor test: \test}
  }
}
\tikzset{
  base/.code={
    \tikzset{/.cd, #1}
    \pgfkeysalso{myshape}
  }
}

\begin{document}
\begin{tikzpicture}
  \node[base={test=changed}] (a){};
  \node at (a.any) {};
\end{tikzpicture}
\end{document}

How can I make all the output to be changed?

I am actually not sure I understand what you wish to achieve here. You have this key, test, and set it initially to some value. Now in the path you set it to another color, and if you use it in the path, you get the updated value. This is a bit similar to setting the draw color, which is initially black, to red in a path. Then the things drawn in that path will be red. However, if you start a new path, and ask the node of the previous path which color it had, you will not be able to find out. Therefore I'd like to ask you to explain what you really want to achieve.

Maybe I went to a wrong direction. Suppose I draw a grid rectangle. I use grid=(2, 3)  to make it a 2x3 grid. Now, I'd like to declare nodes sitting on the grid points in a loop(e.g. for i in {1,2} for j in {1, 2, 3} declare anchor). But I can only get the initial value of grid, so I can't carry out a loop to do this thing.

Probably you know more than I but what doe "But I can only get the initial value of grid" mean? (Also this might be related but as I said I do not yet understand the question.)

As is show in the example above: out side test is initial. Go back to the grid example, let initial value of grid be grid={(1,1)} meaning just a  rectangle without grid lines. Then if I want to perform a loop declaration of grid anchors, number of iterations is always 1.

I see. I think that the answer below can help. (But regardless it can be useful for many related tasks.)

Here is something that can be used to answer the question. Whether or not it will allow you to achieve your ultimate aim, I do not know. What I propose is the following: you could store the local value of test in a macro that has the name of the node in it such that we can associate this value to that specific node. When we call the anchor from outside, we call this macro with the name of the original node, which gets stored in \pgfreferencednodename (see e.g. here).
\documentclass[tikz]{standalone}
\pgfkeys{
  test/.store in=\test,
  test=initial,
}
\makeatletter
\pgfdeclareshape{myshape}{
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \savedanchor{\anyanchor}{
    \typeout{savedanchor test: \test}
    \expandafter\xdef\csname tikz@td@test@\tikz@fig@name\endcsname{\test}
  }
  \saveddimen{\anydimen}{
    \typeout{saveddimen test: \test}
  }
  \typeout{outside test: \test}
  \anchor{any}{
    \anyanchor
    \edef\localtest{\csname tikz@td@test@\pgfreferencednodename\endcsname}
    \typeout{anchor test: \localtest}
  }
}
\tikzset{
  base/.code={
    \pgfkeysalso{myshape}
    \tikzset{/.cd, #1}
  }
}
\makeatother
\begin{document}
\begin{tikzpicture}
  \node[base={test=changed}] (a){};
  \node[base={test=changed}] (b) at (1,0) {};
  \node at (a.any) {};
\end{tikzpicture}
\end{document}

Now the output is
 savedanchor test: changed 
 saveddimen test: changed 
 savedanchor test: changed 
 saveddimen test: changed 
 anchor test: changed

so all is consistent. (In a way this offers the amazing possibility to store anything in a node, e.g. its color. This definitely offers new options for all other ways to make TikZ more versatile for 3D. So thanks for asking this nice question!!! ;-)

Glad to provide some help. It's helpful although the result I want is outside test: changed. But I realize what I want seems to be something that tikz prevents me to do. But with access to the current node's name, I can alter the way to try to name anchors in backgroundpath.
