bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Extract the last n characters from a variable


From: bernard . delechamp
Subject: Extract the last n characters from a variable
Date: Thu, 21 Dec 2000 10:53:37 +0100 (CET)

Configuration Information [Automatically generated, do not change]:
Machine: i586
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i586' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i586-mandrake-linux-gnu' 
-DCONF_VENDOR='mandrake' -DSHELL -DHAVE_CONFIG_H  -D_FILE_OFFSET_BITS=64  -I. 
-I/usr/include -I. -I./lib -I/usr/include -O3 -fomit-frame-pointer 
-fno-exceptions -fno-rtti -pipe -s -mpentium -mcpu=pentium -march=pentium 
-ffast-math -fexpensive-optimizations
uname output: Linux bernard.vaissiere-favre.com 2.2.16 #4 mar aoû 22 09:40:00 
CEST 2000 i686 unknown
Machine Type: i586-mandrake-linux-gnu

Bash Version: 2.03
Patch Level: 19
Release Status: release

Description:
        I need to extract de last 2 characters from a variable.
        So, I try the syntax : ${varname:-2}. I think that there's a
        confusion with ${varname:-default_value}. Examples :

        [bernard@bernard /tmp]$ VAR=tartempion

        I want to obtain the string «on».

        [bernard@bernard /tmp]$ echo ${VAR:-2}
        tartempion

        With a positive offset, it's ok :

        [bernard@bernard /tmp]$ echo ${VAR:2}
        rtempion

Fix:

        The correct result is done by :

        [bernard@bernard /tmp]$ echo ${VAR:$[${#VAR}-2]}
        on

        Excuse my quite bad english, I'm french :)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]