0

I'm trying to do this:

for string in "${STRING[@]}"
do
    LASTCHAR="$(echo \"${string: -1}\")"

    if [[ "$LASTCHAR" == "!" ]]; then
        #do stuff
    fi
done 

What I want to do is check the last character of the string in an if statement. I've searched just about everywhere and no solution I've come across works.

The most prevalent solution or what I thought should have worked is one of the answers to this question.

How to get the last character of a string in a shell?

That question doesn't seem to be asking about doing this from within a script though, just from command line. So I've tried:

LASTCHAR="$(echo \"${string: -1}\")"

This is the closest I can get. This gives an extremely ugly red bar after the : in vim, making me think there is a syntax error.

The script still runs though. And it technically gets the last character of the string, but it also adds double quotes around the character. Every other solution I've tried out there utterly fails.

Community
  • 1
  • 1
krb686
  • 1,726
  • 9
  • 26
  • 46
  • Why do you think that question isn't about doing it within a script? And what difference does it make? The shell syntax is the same in a script and interactively. – Barmar May 22 '15 at 19:40
  • The double quotes are because you're echoing them literally. – Barmar May 22 '15 at 19:42
  • Why do you have `\"` in your `echo` command? – Barmar May 22 '15 at 19:42
  • Please take some time to search in future. I found this link on this very page. You didn't! http://stackoverflow.com/questions/17542892/shell-get-the-last-character-of-a-string?lq=1 – crafter May 22 '15 at 19:45
  • It does not behave the same from within a script unfortunately. For example, removing the escaped double quotes and running: `LASTCHAR="$(echo ${string: -1})"`, and then running `echo $LASTCHAR`, the result is a list of all the files in my current directory – krb686 May 22 '15 at 19:45
  • @crafter Uh what? You've linked the exact same question I already linked above. Did you read where I said I tried the solutions in that question and they don't work? – krb686 May 22 '15 at 19:47
  • Hmm, I'm not sure what the issue is. Maybe my variable isn't a string? If I try instead `myString='hello!' and run it off of myString, it does work from the script. But it doesn't work off my index variable. – krb686 May 22 '15 at 19:54
  • OK, I take back my assertion that you missed the link. You are looking for a cut-and-paste answer, Read and understand. Your quotes are there because you added it. Use $(echo ${PATH: -1}) – crafter May 22 '15 at 19:59
  • @crafter Thanks for trying to help. I still haven't figured out why, but that command does not work. It results in a listing of the files in the current directory. When I `echo $string` I see what I want, a string like `someString*`, and I want to extract the `*`. When I run `LASTCHAR=$(echo ${string: -1})`, and then I run `echo $LASTCHAR` it results in listing all the files in my current dir – krb686 May 26 '15 at 12:40
  • @crafter However, if I add double quotes like the answer in the linked SO post suggests, like this: `echo "${string: -1}"`, this works correctly **in bash** So naturally, I think to add the double quotes around the variable expansion in the script to achieve the same behavior. This does not work. In other words: `LASTCHAR=$(echo "${string: -1}")` and then `echo $LASTCHAR` still results in a listing of the files in current dir. – krb686 May 26 '15 at 12:47
  • @crafter And so my next thought was that there needed to be double quotes around the actual command substitution itself. So you are correct, of course the double quotes are there because I added them. I added them because I thought they were needed. My question wasn't about why the double quotes were there though. – krb686 May 26 '15 at 12:52
  • I am inclined to think it is something wrong with my `string` variable itself. I have up until now assumed it was a string, the same as `myStr='hello*'`, however maybe it isn't. It is being set as the index of a for loop, like this: `for string in "${string[@]}"`, and strings is being set like: `declare -a strings=("$@")`, it is essentially supposed to be an array of strings of all the args passed into the script – krb686 May 26 '15 at 13:01
  • Ah, now I really feel retarded. The `echo $LASTCHAR` needed to be a `echo "$LASTCHAR"` – krb686 May 26 '15 at 13:52

0 Answers0