Skip to content

Instantly share code, notes, and snippets.

@algal
Last active August 8, 2017 00:15
Show Gist options
  • Save algal/7ec026d8a4166b2c5621c7e83380fffd to your computer and use it in GitHub Desktop.
Save algal/7ec026d8a4166b2c5621c7e83380fffd to your computer and use it in GitHub Desktop.
Shell-safe characters
// known-good: Xcode 8.3.3
/*
This shell script outputs which characters are special and need escaping in bash or sh:
#!/bin/bash
special=$'`!@#$%^&*()-_+={}|[]\\;\':",.<>?/ '
for ((i=0; i < ${#special}; i++)); do
char="${special:i:1}"
printf -v q_char '%q' "$char"
if [[ "$char" != "$q_char" ]]; then
printf 'Yes - character %s needs to be escaped\n' "$char"
else
printf 'No - character %s does not need to be escaped\n' "$char"
fi
done | sort
*/
let shellSafe = CharacterSet(charactersIn: "! #$&'()*,;<>?[\\]^`{|}\"").inverted
let urlPathSafe = CharacterSet.urlPathAllowed
let urlFragSafe = CharacterSet.urlFragmentAllowed
let urlQuerySafe = CharacterSet.urlQueryAllowed
let urlSafe = urlPathSafe.intersection(urlFragSafe).intersection(urlQuerySafe)
let filesystemUnsafe = CharacterSet(charactersIn: "/:")
let filesystemSafe = filesystemUnsafe.inverted
let safeEverywhere = urlSafe.intersection(filesystemSafe)
// conclusion: there are no safe-everywhere paired delimiters. ()<>[]{} are all unsafe.
// conclusion: () is URL and fs-safe, but not shell safe.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment