What is the simplest way to remove a trailing slash from each parameter?

前端 未结 6 850
清酒与你
清酒与你 2021-01-30 05:55

What is the simplest way to remove a trailing slash from each parameter in the \'$@\' array, so that rsync copies the directories by name?

rsync -a         


        
6条回答
  •  灰色年华
    2021-01-30 06:48

    FYI, I added these two functions to my .bash_profile based on the answers found on SO. As Chris Johnson said, all answers using ${x%/} remove only one slash, these functions will do what they say, hope this is useful.

    rem_trailing_slash() {
        echo "$1" | sed 's/\/*$//g'
    }
    
    force_trailing_slash() {
        echo "$(rem_trailing_slash "$1")/"
    }
    

提交回复
热议问题