Chinaunix首页 | 论坛 | 博客
  • 博客访问: 340280
  • 博文数量: 245
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: -10
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-24 23:21
文章分类

全部博文(245)

文章存档

2017年(2)

2014年(6)

2013年(1)

2011年(15)

2010年(68)

2009年(153)

分类:

2009-08-17 14:38:29

网上看到老外回答的变量设置方式的区别,挺好,收藏,感谢作者!

> is unset. (Actually, some scripts use "${LD_LIBRARY_PATH+:
> $LD_LIBRARY_PATH}", which seems to work, too. But this is not
> documented in the bash man page, at least I can't find it.)

The difference between ${PARAMETER:+WORD} and ${PARAMETER+WORD} is
subtle, and you're right, it's not documented in the bash man page.
It is part of the POSIX shell standard, though. ${PARAMETER:+WORD}
substitutes WORD if PARAMETER is set and non-empty. ${PARAMETER+WORD}
substitutes WORD if PARAMETER is set, empty or not. For example:

vineet@sprocket:~$ FOO=
vineet@sprocket:~$ echo ${FOO+BAR}
BAR
vineet@sprocket:~$ echo ${FOO:+BAR}

vineet@sprocket:~$ unset FOO
vineet@sprocket:~$ echo ${FOO+BAR}

vineet@sprocket:~$ echo ${FOO:+BAR}

vineet@sprocket:~$

In many cases they'll be equivalent, but in the LD_LIBRARY_PATH case,
I'd recommend using the colon-form. If someone has set an empty
LD_LIBRARY_PATH, the correct behavior is just to add the directory you
want; you don't want to stick an extra empty pathname component in
there.

good times,
Vineet

阅读(461) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~