首页 > 解决方案 > Get expect 取一个带有空格的变量

问题描述

在我的期望脚本中,我有两个要添加的变量。它们都将成为脚本中的变量。

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

在脚本.sh

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

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

目前,

first_var="My"
second_var="Life"

当不将它们作为变量传递时,下面的代码按预期工作。

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

我需要知道如何让该脚本接受变量并且仍然忽略其中的空格。

标签: linuxshell

解决方案


您还必须在引号内传递变量;

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

Bash 扩展的工作方式,这些变量将在脚本运行之前被扩展。就像命令运行一样

./script “我的生活” “很精彩”

参数周围的引号指示外壳不要通过字段分隔符进行拆分。


推荐阅读