How to check the first character in a string in Bash or UNIX shell?

前端 未结 4 420
无人共我
无人共我 2021-01-30 06:07

I\'m writing a script in UNIX where I have to check whether the first character in a string is \"/\" and if it is, branch.

For example I have a string:

/         


        
4条回答
  •  天涯浪人
    2021-01-30 06:43

    $ foo="/some/directory/file"
    $ [ ${foo:0:1} == "/" ] && echo 1 || echo 0
    1
    $ foo="server@10.200.200.20:/some/directory/file"
    $ [ ${foo:0:1} == "/" ] && echo 1 || echo 0
    0
    

提交回复
热议问题