[英]How to make a bash_profile function acts different running within bash_profile or later called by user?
我的意思是,在~/.profile
,函数doit
在用户登录时会说“ Welcome
,但是在用户稍后执行doit
时会说其他的话。
doit() {
if some_test_here; then
echo "Running within ~/.profile. Welcome."
else
echo "Called by user."
fi
}
doit
我认为~/.profile
在Mac上比Linux上的~/.bash_profile
更好。 因此,我以~/.profile
为例。
两种方法是传递参数或检查环境。
使用仅由.profile
的调用使用的参数。
doit () {
if [ "${1:-onlogin}" -eq onlogin ]; then
echo "Running from .profile"
else
echo "Called by user"
fi
}
doit onlogin # from .profile
doit # ordinary call
检查环境中由.profile设置的变量
doit () {
if [ "${_onlogin}" ]; then
echo "Running from .profile"
else
echo "Called by user"
fi
}
onlogin=1 doit # from .profile; value can be any non-empty string
doit # ordinary call
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.