Bash 或等于 ||= 就像 Ruby
Bash 有类似 ||= 的东西吗?
即,是否有更好的方法来执行以下操作:
if [ -z $PWD ]; then PWD=`pwd`; fi
我问是因为我收到此错误:
$ echo ${`pwd`/$HOME/'~'}
-bash: ${`pwd`/$HOME/'~'}: bad substitution
所以,我的计划是:
if [ -z $PWD ]; then PWD=`pwd`; fi
echo ${PWD/$HOME/'~'}
我真正的问题是:“有更好的方法来执行以下操作吗?”
# ~/.bash_profile
# Set prompt to RVM gemset, abbr. of current directory & (git branch).
PROMPT_COMMAND='CUR_DIR=`pwd|sed -e "s!$HOME!~!"|sed -E "s!([^/])[^/]+/!\1/!g"`'
PS1='$(~/.rvm/bin/rvm-prompt g) [$CUR_DIR$(__git_ps1)]\$ '
Does Bash have something like ||= ?
I.e., is there a better way to do the following:
if [ -z $PWD ]; then PWD=`pwd`; fi
I'm asking because I get this error:
$ echo ${`pwd`/$HOME/'~'}
-bash: ${`pwd`/$HOME/'~'}: bad substitution
So, my plan is to do:
if [ -z $PWD ]; then PWD=`pwd`; fi
echo ${PWD/$HOME/'~'}
My real question is: "Is there a better way to do the following?"
# ~/.bash_profile
# Set prompt to RVM gemset, abbr. of current directory & (git branch).
PROMPT_COMMAND='CUR_DIR=`pwd|sed -e "s!$HOME!~!"|sed -E "s!([^/])[^/]+/!\1/!g"`'
PS1='$(~/.rvm/bin/rvm-prompt g) [$CUR_DIR$(__git_ps1)]\$ '
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Bash 允许使用默认值:
如果
$b
未定义,则使用pwd
来分配$a
。Bash allows for default values:
If
$b
is undefined, thenpwd
is used instead in assigning$a
.您可以使用以下命令将提示设置为工作目录:
You can set your prompt to be the working directory with this:
另一个解决方案(在我看来更类似于 Ruby 的或等于)是:
Another solution (which is more akin to Ruby's or-equals in my opinion) would be: