How to get first character of variable

bash

Solution

To get the first character of a variable you need to say:

v="hello"
$ echo "${v:0:1}"
h

However, your code has a syntax error:

[ ! ${line:0:1} == "#"]
#                     ^-- missing space

So this can do the trick:

$ a="123456"
$ [ ! "${a:0:1}" == "#" ] && echo "doesnt start with #"
doesnt start with #
$ a="#123456"
$ [ ! "${a:0:1}" == "#" ] && echo "doesnt start with #"
$ 

Also it can be done like this:

$ a="#123456"
$ [ "$(expr substr $a 1 1)" != "#" ] && echo "does not start with #"
$ 
$ a="123456"
$ [ "$(expr substr $a 1 1)" != "#" ] && echo "does not start with #"
does not start with #

Update

Based on your update, this works to me:

while IFS=$'\n' read line
do
  echo $line
  if [ ! "${line:0:1}" == "#" ] # Error on this line
  then
    eval echo "$line"
    eval createSymlink $line
  fi
done < file

Problem

I'm trying to get the first character of a variable, but I'm getting a Bad substitution error. Can anyone help me fix it? code is: ``` while IFS=$'\n' read line do if [ ! ${line:0:1} == "#"] # Error on this line then eval echo "$line" eval createSymlink $line fi done < /some/file.txt ``` Am I doing something wrong or is there a better way of doing this? -- EDIT -- As requested - here's some sample input which is stored in /some/file.txt ``` $MOZ_HOME/mobile/android/chrome/content/browser.js $MOZ_HOME/mobile/android/locales/en-US/chrome/browser.properties $MOZ_HOME/mobile/android/components/ContentPermissionPrompt.js ```

Original source

Related problems