-1

This is MyScript.sh

I want to execute a script as a text inside my script, I have tried to do it like so:

bash -c "
    #!/bin/bash
    STR=6
    echo $STR
"

Prints empty line.

I tried replacing bash -c with sh -c or eval, all options acts the same, why is that and how can it be solved?

omriman12
  • 1,644
  • 7
  • 25
  • 48

1 Answers1

1

Using single quotes to avoid interpretation in the current shell:

$ cat myscript.sh
bash -c 'STR=6; echo $STR' 
./myscript.sh
6
pifor
  • 7,419
  • 2
  • 8
  • 16