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

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


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

myFunction()
{
    # same as before
}

or:

function myFunction
{
    # same as before
}

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


我遇到了同样的问题,事实上问题不在于函数本身,也不在于函数调用,而在于我传递给函数的参数。

该函数是从脚本的主体中调用的- 'main' -所以我从命令行传递"st1 a b" "st2 c d" "st3 e f",并使用myFunction $*将其传递给函数

$*会导致问题,因为它展开为一组字符,将在函数调用中使用空格作为分隔符进行解释。

解决方案是在显式参数处理中改变对函数的调用,从'main'转向函数:然后调用myFunction "$1" "$2" "$3",这将保留字符串中的空白,因为引号将分隔参数… 因此,如果一个形参可以包含空格,则应该在函数的所有调用中显式地处理它。

由于这可能是对问题进行长时间搜索的原因,因此永远不要使用$*来传递参数可能是明智的…


在初始文本被设置为字符串类型变量的情况下,您可以对这个问题进行扩展,例如:

function status(){    
  if [ $1 != "stopped" ]; then
     artist="ABC";
     track="CDE";
     album="DEF";
     status_message="The current track is $track at $album by $artist";
     echo $status_message;
     read_status $1 "$status_message";
  fi
}

function read_status(){
  if [ $1 != "playing" ]; then
    echo $2
  fi
}

在这种情况下,如果您不将status_message变量作为字符串(由""包围)向前传递,它将被分割成不同参数的挂载。

"$variable":当前轨道是CDE at DEF by ABC

变量:美元


上述问题的另一种解决方案是将每个字符串设置为一个变量,用美元符号\$表示的变量调用函数。然后在函数中使用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

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

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

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


这个问题最简单的解决方案是,当运行shell脚本时,你只需要使用“\”作为空格分隔的参数:

#!/bin/bash
myFunction() {
  echo $1
  echo $2
  echo $3
}
myFunction "firstString" "\"Hello World\"" "thirdString"

更有活力的方法是:

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