0

In my expect script I have two variables I would like to add. Both of them will be variables into the script.

 hello="My Life" 
 world="is wonderful" 
 ./script.sh $hello $world

in script.sh

#!/usr/bin/expect
set timeout 10
match_max 100000000

set first_var [lindex $argv 0]
set second_var [lindex $argv 1]

Currently,

first_var="My"
second_var="Life"

The code below works as expected when don't pass in them in as variables.

./script.sh 'My Life' 'is wonderful'

I need to know how to have that script take in the variables and still ignore white space within them.

pynexj
  • 19,215
  • 5
  • 38
  • 56
Eddie
  • 1,081
  • 2
  • 12
  • 28
  • 1
    use `./script.sh "$hello" "$world"` – Bodo Jul 09 '19 at 15:35
  • Is [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) the best dupe we have for this? It's a bit indirect. – that other guy Jul 09 '19 at 15:38

1 Answers1

2

You must pass the variables within quotes as well;

./script.sh "$hello" "$world"

The way Bash expansion works, these variables will be expanded out before the script is run. It will be the same as if the command were run as

./script "My life" "is wonderful"

The quotes around parameters indicate to the shell not to split via the field separator.

MrW
  • 310
  • 2
  • 7