linux以.开头的变量,linux – Shell =检查变量是否以#开头

一种方法是使用“子串扩展”来切掉变量内容的第一个字符:

if [[ ${x:0:1} == '#' ]]

then

echo 'yep'

else

echo 'nope'

fi

yep

从Bash手册页:

${parameter:offset}

${parameter:offset:length}

Substring Expansion. Expands to up to length characters of

parameter starting at the character specified by offset. If

length is omitted, expands to the substring of parameter start-

ing at the character specified by offset. length and offset are

arithmetic expressions (see ARITHMETIC EVALUATION below).

length must evaluate to a number greater than or equal to zero.

If offset evaluates to a number less than zero, the value is

used as an offset from the end of the value of parameter. If

parameter is @, the result is length positional parameters

beginning at offset. If parameter is an array name indexed by @

or *, the result is the length members of the array beginning

with ${parameter[offset]}. A negative offset is taken relative

to one greater than the maximum index of the specified array.

Note that a negative offset must be separated from the colon by

at least one space to avoid being confused with the :- expan-

sion. Substring indexing is zero-based unless the positional

parameters are used, in which case the indexing starts at 1.

编辑:

当然,如果您转义哈希,您的原始方法会正常工作:

$[[ '#snort' == \#* ]]; echo $?

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值