Extract filename and path from URL in bash script

后端 未结 13 966
别跟我提以往
别跟我提以往 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:44

    In bash:

    URL='http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth'
    URL_NOPRO=${URL:7}
    URL_REL=${URL_NOPRO#*/}
    echo "/${URL_REL%%\?*}"
    

    Works only if URL starts with http:// or a protocol with the same length Otherwise, it's probably easier to use regex with sed, grep or cut ...

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