1
a=HDH b=udud c=jsjsj bash secondscript

The command above works. I'd like to save the assignments in a variable, like so:

value="\
    a=HDH \
    b=udud \
    c=ududj \
    "
$value bash secondscript

But it gives an error:

test.sh: line 9: a=HDH: command not found

Why? What can I do instead?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Golu
  • 350
  • 2
  • 14
  • 1
    Assignments are identified before any expansion happens. – choroba May 27 '20 at 12:01
  • Ok so what should i do then ? Any workaround to achieve my goal – Golu May 27 '20 at 12:02
  • For an explanation of why you can't save assignments in a variable see my writeup here: https://stackoverflow.com/questions/61898254/how-can-i-create-a-bash-environment-variable-that-prefixes-an-environment-variab/61898307#61898307. – John Kugelman May 27 '20 at 12:42
  • `eval $value bash secondscript` – alecxs May 27 '20 at 13:31
  • @alecxs eval is working only when value is an array – Golu May 27 '20 at 14:51
  • i have tested with your example string/variable it works (at least on mksh). maybe you forgot quotes for spaces? https://i.stack.imgur.com/mVnn5.png – alecxs May 27 '20 at 15:21

1 Answers1

3

bash's taking first item a=HDH as a command, what you need is :

value=(
  "a=HDH"
  "b=udud"
  "c=ududj"
)
env "${value[@]}" bash secondscript
Philippe
  • 20,025
  • 2
  • 23
  • 32