0

Given this code, it prints "abc_${var1}" but I want it to print "abc_def" but bash scripting has a lot of little details that I'm not aware of so hopefully someone can explain this behavior:

Shell scripts:

function main()
  var1="def"
  var2=$(get_val_sh ${another_var})

  echo ${var2}
  # I want to echo:
  #   abc_def
  # instead, I get:
  #   abc_${var1}


function get_val_sh()
  var3=call_python(get_val_py())
  echo ${var3}


Python:

def get_val_py():
  print "abc_${var1}"

The other thing is that if I directly return from a bash script (rather than calling Python), I do get the desired result (but I have to call python). Does it have something to do with print in Python returning a different kind of string than echo in bash? I don't even know how to check this.

To be clear, the following code gives the desired result though I have no idea why but can't be used because I need Python to give me the appropriate value:

function main()
  var1="def"
  var2=$(get_val_sh ${another_var})

  echo ${var2}
  # I want to echo:
  #   abc_def
  # instead, I get:
  #   abc_${var1}


function get_val_sh()
  echo "abc_${var1}"
1
  • Could you please keep the code blocks to a single language each? It's already suspicious that you use Python-style indentation in a block labeled “shell scripts”, and it gets downright confusing when we try to reason about who calls whom. Commented Nov 14, 2013 at 7:51

1 Answer 1

1

How are you assigning value to var2?

var1="def"
var3="var1" # var3 contains name of the variable. Note: NOT $var1
var2="abc_${var3}" # This will give abc_var1, not abc_def

Use

var2="abc_${!var3}" # This should give abc_def
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.