1

In PHP, one can copy the variable-value of one variable to another via, for example:

$copy = clone $original;

This is not the place to ask questions about PHP but I misunderstand why adding the word clone there helps, i.e., why not writing only $copy = $original and perhaps a comparison to Bash (if applicable) could help me understand why.

  • 1
    How is this different from y=$x in the shell? Are you also wanting to consider arrays? What's your use case? – Kusalananda Jan 27 '22 at 11:39
  • @they, y=$x only works for scalar variables without attribute (export, integer, nameref...), not for arrays, associative arrays or variables with attributes. Making a copy of (sparse) arrays or associative arrays is particularly painful in ksh/bash. – Stéphane Chazelas Jan 27 '22 at 11:40
  • @StéphaneChazelas The question explicitly says that they want to copy the value, i.e. not necessarily any attributes associated with the variable. I'm going to vote to close until it's been clarified with a real example or until a use case has been supplied. I mean, the question could even be understood as wanting to make a "clone" of a PHP variable, in bash, even though that's nonsensical. – Kusalananda Jan 27 '22 at 11:50
  • @they 1) If there is a change to Y it might effect X 2) maybe, I don't have an opinion about this 3) There is no usecase, I just assume an example in Bash would help me understand why it is done the way it's done in PHP. – variablevaluescloner Jan 27 '22 at 12:19
  • @variablevaluescloner How may a change to some variable x in the shell affect another variable (unless they are explicitly tied to one another, as one may be in some shells, but not in bash, or unless one is a name-reference to the other)? Please show with an example. Are you dealing with name reference variables in bash? – Kusalananda Jan 27 '22 at 12:24
  • @they I don't know, I try to ask if it can happen (and if so, how, in a typical example). – variablevaluescloner Jan 27 '22 at 12:25
  • You are currently asking how to prevent a change in one variable from affecting the value of another. We must assume that you have observed this somewhere and that you're having some issue that depends on variables not being affected by each other. – Kusalananda Jan 27 '22 at 12:27
  • @they I don't ask that or at least didn't mean to, I now understand that I only seek to understand the question in the post title. – variablevaluescloner Jan 27 '22 at 12:29
  • @they I have edited the post to ask a better, more focused question, please respect me by reviewing the post. – variablevaluescloner Jan 27 '22 at 12:31
  • So, this is a PHP question now, and as such it would be better suited for https://stackoverflow.com/ – Kusalananda Jan 27 '22 at 12:35
  • I disagree that "Is there a variable-value clone function in Bash and if not, why not?" is a PHP question but I would agree that since it is based on a PHP usecase, it might need to be closed here. – variablevaluescloner Jan 27 '22 at 12:55
  • 1
    You question is still not clear. We need an example and a use case for what you're trying to do on bash. For instance, what is the content of the $X variable. By default in bash, variable assignment is always like "cloning" in php, only the values are assigned, it's never the reference. Why don't you just try to check this yourself? For instance, X=5; Y="$X"; echo X=$X y=$Y; X=6; echo X=$X y=$Y, you'll see that after changing the value of X, `Y remains the same. But since your question is not clear, I'm not sure if that was your question. – aviro Jan 27 '22 at 13:09
  • 1
    PHP's clone really doesn't have anything to do with Bash (why should Bash, of all languages, be used to explain object-oriented features in PHP?). clone/__clone() is the PHP equivalent of a non-default copy constructor in C++. There's no such thing in Bash as Bash isn't object-oriented. This isn't a Bash question at all and instead it belongs on SO as a PHP question. – TooTea Jan 27 '22 at 20:06

1 Answers1

6

I understand that this cloning (copying) prevents changes to Y to effect on the original value of X.
What is the way to do so in Bash?

I'm not that familiar with PHP, but I'm guessing you want to avoid a situation like this in Python:

>>> a = [1, 2, 3]
>>> b = a
>>> b[1] = 9
>>> b
[1, 9, 3]
>>> a
[1, 9, 3]

Here, a list of three numbers is created, and after the assignment b = a, both names refer to the same list, so changing one changes the other.

A situation like that isn't really possible in Bash, since there are no implicit references, or pretty much any objects to speak of either. If you want a reference to a variable, you need to explicitly use declare -n/typeset -n to make a nameref variable, or use the ${!p} indirect reference.

Also, you can't even copy arrays just like that, by only referencing the array name, you need to do it element by element. If a is an array in Bash, $a just takes the element at index 0:

$ a=(1 2 3)
$ b=$a
$ declare -p b
declare -- b="1"

After the assignment, b is just a scalar (non-array) variable with the value 1.

You could copy an array like so:

$ a=(1 2 3)
$ b=("${a[@]}")
$ declare -p b
declare -a b=([0]="1" [1]="2" [2]="3")

That just copies the values and creates a new array with them. (Similar to b=[*a] in Python, and probably similar to PHP's clone, based on what I gathered.) Also as mentioned in the comments, it forgets the original indexes, which matters if the array is sparse or associative. See Making associative array based on another associative array

ilkkachu
  • 138,973