Skip to main content
added 206 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

As you insert the value new value is ${abc}, the ${abc} part is just a string of characters. This string is not reevaluated for expansions when you do xyz=$@.

To expand ${abc} in the string, do the following in the script:

abc="something='123:234'" eval "xyz=\"$1\"" printf '%s\n' "$xyz" 

The eval takes a piece of shell code. The given code does xyz="$1", but with $1 replaced by the command line argument (the string new value is ${abc}). This statement is then reevaluated by the shell, assigning the correct string to the variable.

As you insert the value new value is ${abc}, the ${abc} part is just a string of characters. This string is not reevaluated for expansions when you do xyz=$@.

To expand ${abc} in the string, do the following in the script:

abc="something='123:234'" eval "xyz=\"$1\"" printf '%s\n' "$xyz" 

The eval takes a piece of shell code. The given code does xyz="$1", but with $1 replaced by the command line argument (the string new value is ${abc}). This is then reevaluated by the shell.

As you insert the value new value is ${abc}, the ${abc} part is just a string of characters. This string is not reevaluated for expansions when you do xyz=$@.

To expand ${abc} in the string, do the following in the script:

abc="something='123:234'" eval "xyz=\"$1\"" printf '%s\n' "$xyz" 

The eval takes a piece of shell code. The given code does xyz="$1", but with $1 replaced by the command line argument (the string new value is ${abc}). This statement is then reevaluated by the shell, assigning the correct string to the variable.

Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

As you insert the value new value is ${abc}, the ${abc} part is just a string of characters. This string is not reevaluated for expansions when you do xyz=$@.

To expand ${abc} in the string, do the following in the script:

abc="something='123:234'" eval "xyz=\"$1\"" printf '%s\n' "$xyz" 

The eval takes a piece of shell code. The given code does xyz="$1", but with $1 replaced by the command line argument (the string new value is ${abc}). This is then reevaluated by the shell.