卷曲后删除Bash中的尾部斜线?

时间:2014-01-28 22:39:49

标签: bash curl

这里有一个奇怪的,Bash版本是4.25,CURL是7.32

据我所知,这是通过终端

运行的脚本(适合你)
 #!/bin/bash

  protocol='https://bing.com'
  location="$(curl -Is $protocol |awk '/Location/{print$2}')" #responds: https://www.bing.com/

    shopt -s extglob;

        echo "'${location}'" #echos: 'https://www.bing.com/
                             #but where's my trailing ' (quote) ????
                             #its completely gone.... ???


    location=$(echo ${location%%*(/)}) #should remove trailing slashes
                                       #but I believe that the missing
                                       #quote from above is causing trouble....


    shopt -u extglob

  echo $location; #should be: https://www.bing.com ???

我无法切断我的尾随斜线,有些东西在干扰,我只是想不出来?

1 个答案:

答案 0 :(得分:3)

每当你的线路末端的某些东西在回声中丢失时,立即想到“回车!”。 echo "$location" | cat -v最后会显示^M

要从HTTP标头卷曲输出中删除回车符,请使用tr -d '\r'

location="$(curl -Is "$protocol" | tr -d '\r' | awk '/Location/{print$2}')"