270

I would like to delete the last character of a string, I tried this little script :

#! /bin/sh 

t="lkj"
t=${t:-2}
echo $t

but it prints "lkj", what I am doing wrong?

serenesat
  • 1,326
user3581976
  • 3,155

13 Answers13

273

With bash 4.2 and above, you can do:

${var::-1}

Example:

$ a=123
$ echo "${a::-1}"
12

Notice that for older bash ( for example, bash 3.2.5 on OS X), you should leave spaces between and after colons:

${var: : -1}
cuonglm
  • 153,898
146

In a POSIX shell, the syntax ${t:-2} means something different - it expands to the value of t if t is set and non null, and otherwise to the value 2. To trim a single character by parameter expansion, the syntax you probably want is ${t%?}

Note that in ksh93, bash or zsh, ${t:(-2)} or ${t: -2} (note the space) are legal as a substring expansion but are probably not what you want, since they return the substring starting at a position 2 characters in from the end (i.e. it removes the first character i of the string ijk).

See the Shell Parameter Expansion section of the Bash Reference Manual for more info:

steeldriver
  • 81,074
112

Using sed it should be as fast as

sed 's/.$//'

Your single echo is then echo ljk | sed 's/.$//'.
Using this, the 1-line string could be any size.

  • 22
    Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string. – Stéphane Chazelas Feb 01 '16 at 10:23
  • 2
    sed -z 's/.$// will do what you're looking for, working with multiple lines too. – TWiStErRob Feb 21 '21 at 11:56
  • 1
    @TWiStErRob I get sed: illegal option -- z on macOS – Andy Jun 14 '21 at 11:30
  • @TWiStErRob: that may remove the last newline... – Luis A. Florit Apr 13 '22 at 22:45
  • Yes, \n is a character, @LuisA.Florit :) You can amend the regex easily if you know more about your input, for example /.\n$/\n/ in your case to keep the last newline and remove the character before the last newline. I think even /.\n?$/\n/ might be an option to ensure a new line at the end of stream. – TWiStErRob Apr 14 '22 at 07:36
86

for removing the last n characters from a line that makes no use of sed OR awk:

> echo lkj | rev | cut -c (n+1)- | rev

so for example you can delete the last character one character using this:

> echo lkj | rev | cut -c 2- | rev

> lk

from rev manpage:

DESCRIPTION
The rev utility copies the specified files to the standard output, reversing the order of characters in every line. If no files are speci- fied, the standard input is read.

UPDATE:

if you don't know the length of the string, try:

$ x="lkj"
$ echo "${x%?}"
lk
Nidal
  • 8,956
66

A few options depending on the shell:

  • POSIX: t=${t%?}
  • Bourne: t=`expr " $t" : ' \(.*\).'`
  • zsh/yash: t=${t[1,-2]}
  • bash/zsh: t=${t:0:-1}
  • ksh93/bash/zsh/mksh: t=${t:0:${#t}-1}
  • ksh93/bash/zsh/mksh: t=${t/%?}
  • ksh93: t=${t/~(E).$/}
  • es: @ {t=$1} ~~ $t *?

Note that while all are supposed to strip the last character, you'll find that some implementations (those that don't support multi-byte characters) strip the last byte instead (so would likely corrupt the last character if it was multi-byte).

The expr variant assumes $t doesn't end in more than one newline character. It will also return a non-zero exit status if the resulting string ends up being 0 (or 000 or even -0 with some implementations). It could also give unexpected results if the string contains invalid characters.

  • Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too! – Russ Sep 16 '16 at 03:20
  • @Russ, t=${t%?} is not Bourne but you're not likely to come across a Bourne shell nowadays. ${t%?} does work in all the other ones though. – Stéphane Chazelas Sep 16 '16 at 06:53
  • No fish shell option given! Probably more popular these days than ksh93... – rien333 Nov 18 '17 at 01:21
  • @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).\z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well. – Stéphane Chazelas Nov 18 '17 at 08:10
  • Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1) – Avindra Goolcharan Apr 05 '18 at 20:41
  • Add fish shell example, not sure if fish has the more native string seeking/manipulation but string trim works for now. – Elijah Lynn Mar 18 '19 at 16:51
  • @ElijahLynn, I've rejected the change as it does something different from the other examples. Removing the last character from a variable seems to be quite difficult with fish builtins if you want to allow the variable to contain newline characters. If you have a solution to remove the last character of $t in fish, feel free to edit it in. – Stéphane Chazelas Mar 18 '19 at 17:20
  • Do not forget to start the script with bash and not sh to make it work and avoid bad subst. – Timo Nov 16 '20 at 13:58
  • 1
    @Timo, not sure what you mean. None of the solutions I gave are bash-specific. The only one invented by bash AFAIK is t=${t:0:-1} (extending the ${var:offset:length} ksh93 operator). For each, I gave which shells support it. You can use the Bourne/POSIX ones from and modern sh. – Stéphane Chazelas Nov 16 '20 at 14:02
  • ok, Stephane, if you use a bash solution like the one with var:offset:length in a script you need to call your script with bash. – Timo Nov 18 '20 at 08:08
54

The most portable, and shortest, answer is almost certainly:

${t%?}

This works in bash, sh, ash, dash, busybox/ash, zsh, ksh, etc.

It works by using old-school shell parameter expansion. Specifically, the % specifies to remove the smallest matching suffix of parameter t that matches the glob pattern ? (ie: any character).

See "Remove Smallest Suffix Pattern" here for a (much) more detailed explanation and more background. Also see the docs for your shell (eg: man bash) under "parameter expansion".


As a side note, if you wanted to remove the first character instead, you would use ${t#?}, since # matches from the front of the string (prefix) instead of the back (suffix).

Also worth noting is that both % and # have %% and ## versions, which match the longest version of the given pattern instead of the shortest. Both ${t%%?} and ${t##?} would do the same as their single operator in this case, though (so don't add the useless extra character). This is because the given ? pattern only matches a single character. Mix in a * with some non-wildcards and things get more interesting with %% and ##.

Understanding parameter expansions, or at least knowing about their existence and knowing how to look them up, is incredibly useful for writing and deciphering shell scripts of many flavors. Parameter expansions often look like arcane shell voodoo to many people because... well... they are arcane shell voodoo (although pretty well documented if you know to look for "parameter expansion"). Definitely good to have in the tool belt when you're stuck in a shell, though.

Russ
  • 903
19
t=lkj
echo ${t:0:${#t}-1}

You get a substring from 0 to the string length -1. Note however that this substraction is bash specific, and won't work on other shells.

For instance, dash isn't able to parse even

echo ${t:0:$(expr ${#t} - 1)}

For example, on Ubuntu, /bin/sh is dash

Volker Siegel
  • 17,283
Ángel
  • 3,589
17

You can also use head to print out all but the last character.

$ s='i am a string'
$ news=$(echo -n $s | head -c -1)
$ echo $news
i am a strin

But unfortunately some versions of head do not include the leading - option. This is the case for the head that comes with OS X.

7

Some refinements. To remove more than one character, you can add multiple question marks. For example, to remove the last two characters from the variable: $SRC_IP_MSG, you can use:

SRC_IP_MSG=${SRC_IP_MSG%??}
Kevdog777
  • 3,224
yuliskov
  • 171
6

It is easy enough to do using regular expression:

n=2
echo "lkj" | sed "s/\(.*\).\{$n\}/\1/"
unxnut
  • 6,008
5

Just to complete some possible usages of pure bash:

#!/bin/bash

# Testing substring removal
STR="Exemple string with trailing whitespace "
echo "'$STR'"
echo "Removed trailing whitespace: '${STR:0:${#STR}-1}'"
echo "Removed trailing whitespace: '${STR/%\ /}'"

The first syntax takes a substring from a string, the syntax is
${STRING:OFFSET:LENGTH}
For the second one, do notice the % sign, which means 'from end of line' and the syntax is
${STRING/PATTERN/SUBSTITUTION}

And here are two shorter forms of the above mentioned

echo "Removed trailing whitespace: '${STR::-1}'"
echo "Removed trailing whitespace: '${STR%\ }'"

Here notice again the % sign, meaning 'Remove ( that is, replace with '' ) the shortest matched pattern (here represented by escaped space '\ ' from the end of the PARAMETER - here named STR

CermakM
  • 273
1

As we can also use php in command line, or shell scripts. It is sometimes useful for surgical parsing.

php -r "echo substr('Hello', 0, -1);" 
// Output hell

With piping:

echo "hello" | php -r "echo substr(trim(fgets(STDIN)), 0, -1);"
// Output hell
NVRM
  • 398
0

In ksh:

echo ${ORACLE_SID/%?/}
techraf
  • 5,941
Alex
  • 11