bash - How to check the first character in a string in unix -


i'm writing script in unix have check whether first character in string "/" , if is, branch.

for example have string

/some/directory/file 

i want return 1

and

server@10.200.200.20:/some/directory/file 

this return 0

how can proceed this?

many ways this. use wildcards in double brackets:

str="/some/directory/file" if [[ $str == /* ]]; echo 1; else echo 0; fi 

you can use substring expansion:

if [[ ${str:0:1} == "/" ]] ; echo 1; else echo 0; fi 

or regex:

if [[ $str =~ ^/ ]]; echo 1; else echo 0; fi 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -