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

标签 bash function

我正在编写 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"
}

和其他人一样,它也适用于我。

关于bash - 在 Bash 中将带空格的字符串作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1983048/

相关文章:

git - 从 PS1 提示符运行 git update-index --refresh 安全吗?

bash - shopt -s dirspell 有什么作用?

r - 处理功能错误,通知然后跳过

javascript - Coffeescript:从同一对象中的函数调用数组函数

function - 有没有办法检查 VBScript 函数是否已定义?

ios - 将现有函数作为参数传递给 Swift 3 中的另一个函数

c++ - i++或++ i更适合此程序吗?

linux - 如何在Linux中重命名所有包含破折号的子目录中的所有.jpg文件?

bash - Grep 不过滤

bash - mv 不识别通配符/变量扩展