-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathurlencode.sh
More file actions
57 lines (51 loc) · 1 KB
/
urlencode.sh
File metadata and controls
57 lines (51 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# ref:https://github.com/sixarm/urlencode.sh
#
# URL encode script for encoding text.
#
# Syntax:
#
# urlencode <string>
#
# Example:
#
# $ urlencode "foo bar"
# foo%20bar
#
# This implementation uses just the shell,
# with no extra dependencies or languages.
#
# Credit:
#
# * https://gist.github.com/cdown/1163649
#
# Links:
#
# * https://github.com/sixarm/urlencode.sh
# * https://github.com/sixarm/urldecode.sh
#
# Command: urlencode
# Version: 1.0.0
# Created: 2016-09-12
# Updated: 2016-09-12
# License: MIT
# Contact: Joel Parker Henderson (joel@joelparkerhenderson.com)
##
urlencode() {
# urlencode <string>
old_lang=$LANG
LANG=C
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LANG=$old_lang
LC_COLLATE=$old_lc_collate
}
urlencode "$1"