我正在编写一个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的定义是错误的。它应该是:

myFunction()
{
    # same as before
}

or:

function myFunction
{
    # same as before
}

不管怎样,它在Bash 3.2.48上看起来很好,对我来说也很好。

其他回答

对我来说很简单的解决方案——引用$@

Test(){
   set -x
   grep "$@" /etc/hosts
   set +x
}
Test -i "3 rb"
+ grep -i '3 rb' /etc/hosts

我可以验证实际的grep命令(多亏了set -x)。

你应该加上引号,而且,你的函数声明是错误的。

myFunction()
{
    echo "$1"
    echo "$2"
    echo "$3"
}

和其他人一样,这对我也适用。

你对myFunction的定义是错误的。它应该是:

myFunction()
{
    # same as before
}

or:

function myFunction
{
    # same as before
}

不管怎样,它在Bash 3.2.48上看起来很好,对我来说也很好。

更有活力的方法是:

function myFunction {
   for i in "$*"; do echo "$i"; done;
}

上述问题的另一种解决方案是将每个字符串设置为一个变量,用美元符号\$表示的变量调用函数。然后在函数中使用eval读取变量并按预期输出。

#!/usr/bin/ksh

myFunction()
{
  eval string1="$1"
  eval string2="$2"
  eval string3="$3"

  echo "string1 = ${string1}"
  echo "string2 = ${string2}"
  echo "string3 = ${string3}"
}

var1="firstString"
var2="second string with spaces"
var3="thirdString"

myFunction "\${var1}" "\${var2}" "\${var3}"

exit 0

然后输出:

    string1 = firstString
    string2 = second string with spaces
    string3 = thirdString

在尝试解决类似的问题时,我遇到了UNIX认为我的变量是空间定界的问题。我试图将一个用管道分隔的字符串传递给一个使用awk的函数,以设置稍后用于创建报告的一系列变量。我最初尝试了ghostdog74发布的解决方案,但不能让它工作,因为不是我所有的参数都在引号中传递。在给每个参数添加双引号之后,它就可以正常工作了。

下面是我的代码之前的状态和之后的状态。

之前-非功能代码

#!/usr/bin/ksh

#*******************************************************************************
# Setup Function To Extract Each Field For The Error Report
#*******************************************************************************
getField(){
  detailedString="$1"
  fieldNumber=$2

  # Retrieves Column ${fieldNumber} From The Pipe Delimited ${detailedString} 
  #   And Strips Leading And Trailing Spaces
  echo ${detailedString} | awk -F '|' -v VAR=${fieldNumber} '{ print $VAR }' | sed 's/^[ \t]*//;s/[ \t]*$//'
}

while read LINE
do
  var1="$LINE"

  # Below Does Not Work Since There Are Not Quotes Around The 3
  iputId=$(getField "${var1}" 3)
done<${someFile}

exit 0

后功能代码

#!/usr/bin/ksh

#*******************************************************************************
# Setup Function To Extract Each Field For The Report
#*******************************************************************************
getField(){
  detailedString="$1"
  fieldNumber=$2

  # Retrieves Column ${fieldNumber} From The Pipe Delimited ${detailedString} 
  #   And Strips Leading And Trailing Spaces
  echo ${detailedString} | awk -F '|' -v VAR=${fieldNumber} '{ print $VAR }' | sed 's/^[ \t]*//;s/[ \t]*$//'
}

while read LINE
do
  var1="$LINE"

  # Below Now Works As There Are Quotes Around The 3
  iputId=$(getField "${var1}" "3")
done<${someFile}

exit 0