特殊字符和解析BUG – Bashscript

Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for ". (The text was '"cpulimit') 

这是我在终端中运行以下脚本时得到的

  #!/bin/bash read -p "Which program u want to limit its processes?" ProgrameName read -p "Which limitation percentage u want for it ?" limitationPercentage getAllPIDRunUnderThisProgram=$( ps -e | grep "$ProgrameName" | awk '{print $1;}') for i in $getAllPIDRunUnderThisProgram do gnomeTab+=" --tab -e \"cpulimit -p $i -l $limitationPercentage \" " done gnome-terminal $gnomeTab 

由于第8行gnomeTab+=" --tab -e \"cpulimit -p $i -l $limitationPercentage \" "中的双引号,他无法解析必须使用的转义字符“\ gnomeTab+=" --tab -e \"cpulimit -p $i -l $limitationPercentage \" " ,所以有没有解决方案使用双引号,因为它们必须在--tab -e " some commands "而不是解决问题?

您可以将第一行更改为

 #!/bin/bash -xv 

使shell显示它如何解释参数。

您应该使用数组来累积选项,而不是转义(导致eval ):

 for i in $getAllPIDRunUnderThisProgram ; do gnomeTab+=(--tab -e "cpulimit -p $i -l $limitationPercentage") done echo "${gnomeTab[@]}"