96

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:

/some/directory/file

I want this to return 1, and:

server@10.200.200.20:/some/directory/file

to return 0.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
canecse
  • 1,593
  • 2
  • 13
  • 20
  • [How to check if a string begins with some value in Bash?](https://stackoverflow.com/q/2172352/608639). – jww Nov 01 '19 at 16:32

5 Answers5

167

There are 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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
user000001
  • 30,389
  • 12
  • 73
  • 103
19

Consider the case statement as well which is compatible with most sh-based shells:

case $str in
/*)
    echo 1
    ;;
*)
    echo 0
    ;;
esac
konsolebox
  • 66,700
  • 11
  • 93
  • 101
11
$ 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
devnull
  • 111,086
  • 29
  • 224
  • 214
  • 5
    Downvote! You forgot to quote your variable in single braces. ``foo='*'; [ ${foo:0:1} == "/" ] && echo 1 || echo 0`` is going to output this error ``bash: [: too many arguments``. Use ``[ "${foo:0:1}" == "/" ] && echo 1 || echo 0`` or even better ``[[ ${foo:0:1} == "/" ]] && echo 1 || echo 0`` – Aleks-Daniel Jakimenko-A. Aug 28 '13 at 14:31
  • @Aleks-DanielJakimenko-A. - devnull's answer works without error in "GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2013 Free Software Foundation, Inc." – Craig Hicks Apr 20 '18 at 11:20
  • Your initial expression: "Downvote! You forgot to quote your variable in single braces." implies that the expressions entered in devnulls answer won't work as is on bash. You need to modify that initial expression in order to properly deliver your technical point, otherwise it isn't logically correct. – Craig Hicks Apr 21 '18 at 16:49
  • 1
    @Aleks-DanielJakimenko-A. Downvote on a typo? Really? – Mausy5043 Aug 05 '18 at 06:56
  • 3
    @Mausy5043 it's not a “typo”, it is buggy and incorrect. – Aleks-Daniel Jakimenko-A. Aug 06 '18 at 08:57
8

cut -c1

This is POSIX, and unlike case, it actually extracts the first character if you need it for later:

myvar=abc
first_char="$(printf '%s' "$myvar" | cut -c1)"
if [ "$first_char" = a ]; then
  echo 'starts with a'
else
  echo 'does not start with a'
fi

awk substr is another POSIX command, but less efficient alternative:

printf '%s' "$myvar" | awk '{print substr ($0, 0, 1)}'

printf '%s' is to avoid problems with escape characters: Bash printf literal verbatim string, e.g.,

myvar='\n'
printf '%s' "$myvar" | cut -c1

outputs \ as expected.

${::} does not seem to be POSIX.

See also: How can I extract the first two characters of a string in shell scripting?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
1

Code:

 place="Place"
 fchar=${place:0:1}
 echo $fchar

Output:

P
naqviO7
  • 21
  • 2