Skip to main content
Post Reopened by Stephen Kitt, Jeff Schaller, G-Man Says 'Reinstate Monica', Anthony Geoghegan, Stéphane Chazelas
added 9 characters in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

This is what I wannawant to achieve:

Function:

Func1() {
   $1="Hello World"
}

Call function:

local var1
Func1 var1
echo $var1  (should echo Hello World)

I found this example which seems to work, but I guess using eval is not a good idea:

Func1() {
   eval $1=$str1
}

How would be the correct way of doing this?

I'm coming from .Net and often use a parameter as a reference....i.e. For example, assigning a value back to the parameter which then can be used later on.

In the above example var1 should be assigned "Hello World"

(I'm using sh shell)

This is what I wanna achieve:

Function:

Func1() {
   $1="Hello World"
}

Call function:

local var1
Func1 var1
echo $var1  (should echo Hello World)

I found this example which seems to work but I guess using eval is not a good idea:

Func1() {
   eval $1=$str1
}

How would be the correct way of doing this?

I'm coming from .Net and often use a parameter as a reference....i.e. assigning a value back to the parameter which then can be used later on.

In the above example var1 should be assigned "Hello World"

(I'm using sh shell)

This is what I want to achieve:

Function:

Func1() {
   $1="Hello World"
}

Call function:

local var1
Func1 var1
echo $var1  (should echo Hello World)

I found this example which seems to work, but I guess using eval is not a good idea:

Func1() {
   eval $1=$str1
}

How would be the correct way of doing this?

I'm coming from .Net and often use a parameter as a reference. For example, assigning a value back to the parameter which then can be used later on.

In the above example var1 should be assigned "Hello World"

(I'm using sh shell)

added 194 characters in body
Source Link
MrCalvin
  • 776
  • 1
  • 9
  • 26

This is what I wanna achieve:

Function:

Func1() {
   str1=$2
  $1="Hello $1=$str1World"
}

Call function:

local var1
Func1 var1 "Helle World"
echo $var1  (should echo Hello World)

I knowfound this willexample which seems to work but I guess it'susing eval is not very "pretty"a good idea:

Func1() {
   str1=$2
   eval $1=$str1
}

How would be the correct way be of doing this?

I'm coming from .Net and often use a parameter as a reference....i.e. assigning a value back to the parameter which then can be used later on.

In the above example var1 should be assigned "Hello World"

(I'm using sh shell)

Function:

Func1() {
   str1=$2
   $1=$str1
}

Call function:

local var1
Func1 var1 "Helle World"
echo $var1

I know this will work but I guess it's not very "pretty":

Func1() {
   str1=$2
   eval $1=$str1
}

How would the correct way be of doing this?

(I'm using sh shell)

This is what I wanna achieve:

Function:

Func1() {
   $1="Hello World"
}

Call function:

local var1
Func1 var1
echo $var1  (should echo Hello World)

I found this example which seems to work but I guess using eval is not a good idea:

Func1() {
   eval $1=$str1
}

How would be the correct way of doing this?

I'm coming from .Net and often use a parameter as a reference....i.e. assigning a value back to the parameter which then can be used later on.

In the above example var1 should be assigned "Hello World"

(I'm using sh shell)

Post Closed as "Needs details or clarity" by Thomas Dickey, GAD3R, G-Man Says 'Reinstate Monica', Julie Pelletier, Archemar
edited tags; edited title
Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

Save return-value value from a function in one of it'sits own parameters

Source Link
MrCalvin
  • 776
  • 1
  • 9
  • 26
Loading