How to cut the last field from a shell string

前端 未结 5 979
孤独总比滥情好
孤独总比滥情好 2020-12-14 07:43

How to cut the last field in this shell string

LINE=\"/string/to/cut.txt\"

So that the string would look like this

LINE=\"/         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 08:32

    This will work in modern Bourne versions such as Dash, BusyBox ash, etc., as well as descendents such as Bash, Korn shell and Z shell.

    LINE="/string/to/cut.txt"
    LINE=${LINE%/*}
    

    or to keep the final slash:

    LINE=${LINE%/*}/
    

提交回复
热议问题