-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlEncode.sh
More file actions
22 lines (18 loc) · 723 Bytes
/
urlEncode.sh
File metadata and controls
22 lines (18 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Encode String with urlencoding / only special chars
urlencode() {
local string="${1}";
local strlen=${#string};
local encoded="";
local pos c o;
for (( pos=0 ; pos<strlen ; pos++ ));
do c=${string:$pos:1};
case "$c" in [-_.~a-zA-Z0-9] )
o="${c}" ;; * )
printf -v o '%%%02x' "'$c";
esac; encoded+="${o}";
done;
echo "${encoded}"| sed 's/\./%2e/g';
}
Example
urlencode "https://www.msn.com/this/that?other=thing"
https%3a%2f%2fwww%2emsn%2ecom%2fthis%2fthat%3fother%3dthing