Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first character of variable

Tags:

bash

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
like image 938
Martyn Avatar asked Jul 18 '13 12:07

Martyn


People also ask

How do you extract the first character of a string?

To get the first and last characters of a string, use the charAt() method, e.g. str. charAt(0) returns the first character, whereas str. charAt(str. length - 1) returns the last character of the string.

Can a number be the first character in a variable?

- Numbers are not allowed as the first character. - Variable names cannot include a mathematical or logical operator in the name. Variables can start only a latter or underscore(_) can't start with numbers sign or keywords. you are wrong bro, only letters or underscore must used to start variables.

How do you select the first character in R?

We can get the first character of a string by using the built-in substr() function in R. The substr() function takes 3 arguments, the first one is a string, the second is start position, third is end position.


2 Answers

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
like image 167
fedorqui 'SO stop harming' Avatar answered Nov 16 '22 02:11

fedorqui 'SO stop harming'


Adding the missing space (as suggested in fedorqui's answer ;) ) works for me.

An alternative method/syntax

Here's what I would do in Bash if I want to check the first character of a string

if [[ $line != "#"* ]]

On the right hand side of ==, the quoted part is treated literally whereas * is a wildcard for any sequence of character.

For more information, see the last part of Conditional Constructs of Bash reference manual:

When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in Pattern Matching

Checking that you're using the right shell

If you are getting errors such as "Bad substitution error" and "[[: not found" (see comment) even though your syntax is fine (and works fine for others), it might indicate that you are using the wrong shell (i.e. not Bash).

So to make sure you are using Bash to run the script, either

  • make the script executable and use an appropriate shebang e.g. #!/bin/bash
  • or execute it via bash my_script

Also note that sh is not necessarily bash, sometimes it can be dash (e.g. in Ubuntu) or just plain ol' Bourne shell.

like image 40
doubleDown Avatar answered Nov 16 '22 03:11

doubleDown