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