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
y=$x
in the shell? Are you also wanting to consider arrays? What's your use case? – Kusalananda Jan 27 '22 at 11:39y=$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:40bash
, even though that's nonsensical. – Kusalananda Jan 27 '22 at 11:50x
in the shell affect another variable (unless they are explicitly tied to one another, as one may be in some shells, but not inbash
, or unless one is a name-reference to the other)? Please show with an example. Are you dealing with name reference variables inbash
? – Kusalananda Jan 27 '22 at 12:24bash
. For instance, what is the content of the$X
variable. By default inbash
, 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 ofX
, `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:09clone
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