简体   繁体   中英

Is it possible to detect a space at the end of an argument with a bash function?

I have a bash function that looks like this:

test5() { echo $#; }

I want to be able to detect when there is a space after the last argument.

For example, running this (quotes show to illustrate whitespace): "test5 test" produces: 1 while "test5 test " also produces 1 . I would like to detect when there is a space after test.

Thank you!

Won't work from commandline. May work in script:

#/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     "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM