加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash中传递带有空格的字符串作为函数参数

发布时间:2020-12-15 16:48:20 所属栏目:安全 来源:网络整理
导读:我写一个bash脚本,我需要传递一个字符串包含空格到我的bash脚本中的函数。 例如: #!/bin/bashmyFunction{ echo $1 echo $2 echo $3}myFunction "firstString" "second string with spaces" "thirdString" 运行时,我期望的输出是: firstStringsecond stri
我写一个bash脚本,我需要传递一个字符串包含空格到我的bash脚本中的函数。

例如:

#!/bin/bash

myFunction
{
    echo $1
    echo $2
    echo $3
}

myFunction "firstString" "second string with spaces" "thirdString"

运行时,我期望的输出是:

firstString
second string with spaces
thirdString

然而,实际输出是:

firstString
second
string

有没有办法传递带有空格的字符串作为bash中的函数的单个参数?

你应该把引号,而且,你的函数声明是错误的。
myFunction()
{
    echo "$1"
    echo "$2"
    echo "$3"
}

和其他人一样,它也适用于我。告诉我们您使用的是什么版本的shell。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读