Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove %0D from variable in BASH

Tags:

I'm trying this:

token=`curl -I --header "X-Auth-User: [email protected]" --header "X-Auth-Key: XXXXXXXXXXXXXXXXXXXXXX" api.server.com | grep -Fi X-Auth-Token | awk -F" " '{ print $2}'`  /usr/bin/wget --accept .jpg,.jpeg -p "https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=$token" -O "image.jpg" || rm "image.jpg" 

But my token result is:

https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=SERVER_018d8100000000001d1b817f7d58a6%0D 

Instead of:

https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=SERVER_018d8100000000001d1b817f7d58a6 

How to remove %0D (Carriage return)?

like image 867
rdshck Avatar asked Nov 25 '13 05:11

rdshck


People also ask

How do I remove a character from a variable in bash?

To remove the last n characters of a string, we can use the parameter expansion syntax ${str::-n} in the Bash shell. -n is the number of characters we need to remove from the end of a string.

How do I remove a quote from a variable in bash?

A simple and elegant answer from Stripping single and double quotes in a string using bash / standard Linux commands only: BAR=$(eval echo $BAR) strips quotes from BAR . If you don't want anything printed out, you can pipe the evals to /dev/null 2>&1 .

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do I strip a string in bash?

Use sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]]. $ echo "$Var are very popular now."


2 Answers

You can add | tr -d '\r' to your curl pipeline to strip any carriage returns.

like image 129
that other guy Avatar answered Sep 17 '22 16:09

that other guy


There is a utility called dos2unix. You may have to install it. or use the translate

tr -d '\r' < input > output 

EDIT Found a post that discusses a few options: Remove carriage return in Unix

like image 37
crafter Avatar answered Sep 17 '22 16:09

crafter