【发布时间】:2015-05-23 01:33:46
【问题描述】:
我正在使用以下作为 git shell 脚本。
#!/bin/sh
[ -z "$1" ] && dir="$PWD" || dir="$1"
find "$dir" -mindepth 1 -maxdepth 1 -type d -exec sh -c '
for i; do
printf "\n%s\n" "$i"
cd "$i" \
&& git remote update
done' sh {} +
但想使用 git 别名。
rmup=!sh -c "find . -mindepth 1 -maxdepth 1 -type d -exec sh -c '\
for i; do \
printf "\n%s\n" "$i" && cd "$i" && git remote update; \
done' sh {} +"
或者至少是一个 shell 别名。
alias rmup="find . -mindepth 1 -maxdepth 1 -type d -exec sh -c '\
for i; do \
printf "\n%s\n" "$i" && cd "$i" && git remote update; \
done' sh {} +"
我尝试了各种转义、单引号和双引号组合,但都没有成功。
想要使用“嵌入式”别名的原因是为了简化机器之间的维护和可移植性,因为我已经在同步我的 .gitconfig 和 .bashrc。
忘了说我也试过一个shell函数,当printf打印目录时,cd抱怨目录不存在。
rmup() {
find . -mindepth 1 -maxdepth 1 -type d -exec sh -c '
for i; do
printf "\n%s\n" "$i" && cd "$i" && git remote update;
done' sh {} +
}
【问题讨论】:
-
如果您已经有一个可以工作的 shell 脚本,为什么还需要一个 shell 别名?
-
为什么要使用 shell 别名而不是 shell 函数?