Add entries for URL encoding and decoding
This commit is contained in:
@@ -308,6 +308,55 @@ $ rstrip "The Quick Brown Fox" " Fox"
|
||||
The Quick Brown
|
||||
```
|
||||
|
||||
## Percent-encode a string
|
||||
|
||||
**Example Function:**
|
||||
|
||||
```sh
|
||||
urlencode() {
|
||||
# Usage: urlencode "string"
|
||||
for (( i = 0; i < ${#1}; i++ )); do
|
||||
: "${1:i:1}"
|
||||
case "$_" in
|
||||
[a-zA-Z0-9.~_-])
|
||||
printf '%s' "$_"
|
||||
;;
|
||||
|
||||
*)
|
||||
printf '%%%02X' "'$_"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
printf '\n'
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ urlencode "https://github.com/dylanaraps/pure-bash-bible"
|
||||
https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible
|
||||
```
|
||||
|
||||
## Decode a percent-encoded string
|
||||
|
||||
**Example Function:**
|
||||
|
||||
```sh
|
||||
urldecode() {
|
||||
# Usage: urldecode "string"
|
||||
: "${1//+/ }"
|
||||
printf '%b\n' "${_//%/\\x}"
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
|
||||
https://github.com/dylanaraps/pure-bash-bible
|
||||
```
|
||||
|
||||
## Check if string contains a sub-string
|
||||
|
||||
**Using a test:**
|
||||
|
||||
@@ -21,7 +21,7 @@ $ hello_world="value"
|
||||
$ var="world"
|
||||
|
||||
# Declare a nameref.
|
||||
$ declare -n "ref=hello_$var"
|
||||
$ declare -n ref=hello_$var
|
||||
|
||||
$ printf '%s\n' "$ref"
|
||||
value
|
||||
@@ -31,7 +31,7 @@ value
|
||||
|
||||
```shell
|
||||
$ var="world"
|
||||
$ declare hello_$var=value
|
||||
$ declare "hello_$var=value"
|
||||
$ printf '%s\n' "$hello_world"
|
||||
value
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user