Remove a character from the end of a variable

后端 未结 4 2053
温柔的废话
温柔的废话 2020-12-07 14:17

Bash auto completion appends a / at the end of a directory name. How I can strip this off from a positional parameter?

#!/bin/sh

target=$1

function backup()         


        
相关标签:
4条回答
  • 2020-12-07 14:54

    Use

    target=${1%/}
    

    A reference.

    0 讨论(0)
  • 2020-12-07 15:02

    Use target=${1%/}

    See this the parameter substitution of this bash scripting guide for more.

    0 讨论(0)
  • 2020-12-07 15:06

    Be careful, bash3 added perl-similar regex to bash. The guide mentioned covers this as well as the official guide at GNU , but not all references do.

    What did I do?

    Substitute 2.19/* to be 2.19.

    Solution

    VER="2.19/foo-bar"
    NEWVER=${VER%/*}
    
    0 讨论(0)
  • 2020-12-07 15:10

    I think better solution to canonize paths is realpath $path or with -m option if it doesn't exist. This solution automaticaly removes unnecessary slashes and adds pwd

    0 讨论(0)
提交回复
热议问题