linux - 是否可以使用 bash 函数检测参数末尾的空格?

标签 linux bash shell

我有一个如下所示的 bash 函数:

test5() { echo $#; }

我希望能够检测到最后一个参数后面何时有空格。

例如,运行此命令(引号显示以说明空格):"test5 test" 会生成:1,而 "test5 test " 也会生成 1。我想检测测试后何时有空格。

谢谢!

最佳答案

无法从命令行运行。可以在脚本中工作:

#/bin/bash
test5(){
    [[ $(sed ${BASH_LINENO[0]}'!d;/ $/!d' <"${BASH_SOURCE[1]}") ]] &&
    echo called with trailing whitespace

    echo $#;
}

test5 5 args no trailing spaces
test5 5 args three trailing spaces   
test5 "1 arg no trailing spaces     "

关于linux - 是否可以使用 bash 函数检测参数末尾的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54845773/

相关文章:

c - 如何解决链接描述文件中的错误?

linux - 如何在 shell 脚本中回显动态变量的内容

linux - Expect 命令在 Linux 中再次要求输入密码

c - 使用 cURL 为自定义请求 header 发送空字符串值

c - argc、argv 及其用法的有趣观察

linux - 无法打开 uid_map 以从具有 cap_setuid 功能集的应用程序写入

Java 的 Scanner 类 : using left- and right buttons with Bash

linux - 如何结合 echo 和查找?

linux - awk 使用外部脚本和 <

linux - grep -Po '...\K...' 有什么作用?否则如何达到这种效果?