Skip to content

Instantly share code, notes, and snippets.

@gintenlabo
Last active December 20, 2023 10:30
Show Gist options
  • Save gintenlabo/a09343702b790dc898b981db0ca45cc6 to your computer and use it in GitHub Desktop.
Save gintenlabo/a09343702b790dc898b981db0ca45cc6 to your computer and use it in GitHub Desktop.
与えられた各引数をエスケープした上でスペース区切りで出力する関数
#!/usr/bin/env bash
set -ueo pipefail
SEPARATOR=${SEPARATOR:- }
TRAILING_SEPARATOR=${TRAILING_SEPARATOR:-}
quote_each_args() {
for i in $(seq 1 $#); do
printf '%q' "${!i}"
if [[ $i -lt $# ]]; then
printf '%s' "${SEPARATOR}"
else
printf '%s' "${TRAILING_SEPARATOR}"
fi
done
}
# usage
print_command() {
echo "command: '$(quote_each_args "$@")'"
}
print_command rm -f 'file with space'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment