Linux bash中CLI将接受带空格的参数的正确语法
你会得到Linux bash中CLI将接受带空格的参数的正确语法,linux,bash,shell,command-line-interface,command-line-arguments,Linux,Bash,Shell,Command Line Interface,Command Line Arguments,你会得到 bash testexp.sh -p sherman,wilson, taylor 但是taylor不会被包括在内,因为他的名字没有被添加到数组变量中 如何使cli解析器忽略额外的空间,以便输出如下所示 sherman wilson 使用撇号: sherman wilson taylor 使用撇号: sherman wilson taylor bash就是这样工作的:空格分隔单词。如果您需要空格作为单词的一部分,则必须使用引号,或用反斜杠转义空格。与问题无关,但我建议添加适当的
bash testexp.sh -p sherman,wilson, taylor
但是taylor不会被包括在内,因为他的名字没有被添加到数组变量中
如何使cli解析器忽略额外的空间,以便输出如下所示
sherman
wilson
使用撇号:
sherman
wilson
taylor
使用撇号:
sherman
wilson
taylor
bash就是这样工作的:空格分隔单词。如果您需要空格作为单词的一部分,则必须使用引号,或用反斜杠转义空格。与问题无关,但我建议添加适当的shebang行(
#!/bin/bash
或#!/usr/bin/env bash
,注意它必须是脚本中的第一行),使脚本可执行(chmod+x testexp.sh
),然后不使用bash
命令直接运行它(/testexp.sh-p…
)。另请参见,(U&L.SE),(SO),(AskU),等等。bash就是这样工作的:空格分隔单词。如果您需要空格作为单词的一部分,则必须使用引号,或用反斜杠转义空格。与问题无关,但我建议添加一个适当的shebang行(!/bin/bash
或!/usr/bin/env bash
,请注意它必须是脚本中的第一行),使脚本可执行(chmod+x testexp.sh
),然后在不使用bash
命令(/testexp.sh-p…
)的情况下直接运行它。另请参见,(U&L.SE),(SO),(AskU),等等。双引号也可以。您还可以跳出空格:bash testexp.sh-p sherman,wilson,\taylor
双引号也可以。您还可以跳出空格:bash testexp.sh-p sherman,wilson,\taylor
bash testexp.sh -p 'sherman,wilson, taylor'