Extract filename and path from URL in bash script

后端 未结 13 1328
别跟我提以往
别跟我提以往 2021-01-30 14:17

In my bash script I need to extract just the path from the given URL. For example, from the variable containing string:

http://login:password@example.com/one/more/dir/fi

13条回答
  •  天命终不由人
    2021-01-30 14:43

    I agree that "cut" is a wonderful tool on the command line. However, a more purely bash solution is to use a powerful feature of variable expansion in bash. For example:

    pass_first_last='password,firstname,lastname'
    
    pass=${pass_first_last%%,*}
    
    first_last=${pass_first_last#*,}
    
    first=${first_last%,*}
    
    last=${first_last#*,}
    
    or, alternatively,
    
    last=${pass_first_last##*,}
    

提交回复
热议问题