How to check the first character in a string in unix

来源:互联网 发布:linux cut uniq 编辑:程序博客网 时间:2024/05/16 15:57

Many ways to do this. You could use wildcards in double brackets:
str="/some/directory/file"


if [[ $str == /* ]]; then echo 1; else echo 0; fi


You can use substring expansion:
if [[ ${str:0:1} == "/" ]] ; then echo 1; else echo 0; fi


Or a regex:
if [[ $str =~ ^/ ]]; then echo 1; else echo 0; fi

0 0
原创粉丝点击