传递一个字符串空格作为在bash函数参数 [英] Passing a string with spaces as a function argument in bash
本文介绍了传递一个字符串空格作为在bash函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在写一个bash脚本,我需要传递包含空格的函数在我的bash脚本的字符串。
I'm writing a bash script where I need to pass a string containing spaces to a function in my bash script.
例如:
#!/bin/bash
myFunction
{
echo $1
echo $2
echo $3
}
myFunction "firstString" "second string with spaces" "thirdString"
在运行,我期望的输出是:
When run, the output I'd expect is:
firstString
second string with spaces
thirdString
然而,什么是真正的输出是:
However, what's actually output is:
firstString
second
string
有没有办法用空格传递字符串作为单个参数的函数在bash?
Is there a way to pass a string with spaces as a single argument to a function in bash?
推荐答案
你应该把报价和也,你的函数声明是错误的。
you should put quotes and also, your function declaration is wrong.
myFunction()
{
echo "$1"
echo "$2"
echo "$3"
}
和其他人一样,我的作品也是如此。请告诉我们您使用的是什么版本的外壳。
And like the others, it works for me as well. Tell us what version of shell you are using.
这篇关于传递一个字符串空格作为在bash函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文