Bash 带有嵌套引号的别名
Bash alias with nested quotations
如何为这个命令设置别名? (因为它有多个引号)
rsync -azv -e 'ssh -o "ProxyCommand ssh -A some@place -W %h:%p"' user@xxx:/data/as ~/
只需使用单引号并将每个单引号替换为 '\''
。
alias XYZ='rsync -azv -e '\''ssh -o "ProxyCommand ssh -A some@place -W %h:%p"'\'' user@xxx:/data/as ~/'
或者,使用函数而不是别名
XYZ () {
rsync -azv -e 'ssh -o "ProxyCommand ssh -A some@place -W %h:%p"' user@xxx:/data/as ~/ "$@"
}
它更灵活,让您有机会稍后对命令进行参数化。
转义内部双引号并用双引号引用所有内容,因为您无法转义单引号而只能转义双引号。 (听起来有点好笑)
alias foobar="rsync -azv -e 'ssh -o \"ProxyCommand ssh -A some@place -W %h:%p\"' user@xxx:/data/as ~/"
您可能需要查看 this 答案。
如何为这个命令设置别名? (因为它有多个引号)
rsync -azv -e 'ssh -o "ProxyCommand ssh -A some@place -W %h:%p"' user@xxx:/data/as ~/
只需使用单引号并将每个单引号替换为 '\''
。
alias XYZ='rsync -azv -e '\''ssh -o "ProxyCommand ssh -A some@place -W %h:%p"'\'' user@xxx:/data/as ~/'
或者,使用函数而不是别名
XYZ () {
rsync -azv -e 'ssh -o "ProxyCommand ssh -A some@place -W %h:%p"' user@xxx:/data/as ~/ "$@"
}
它更灵活,让您有机会稍后对命令进行参数化。
转义内部双引号并用双引号引用所有内容,因为您无法转义单引号而只能转义双引号。 (听起来有点好笑)
alias foobar="rsync -azv -e 'ssh -o \"ProxyCommand ssh -A some@place -W %h:%p\"' user@xxx:/data/as ~/"
您可能需要查看 this 答案。