Bash Remove Last Character From String / Line / Word

I have a file of that looks as follows:

foo bar
tom jerry
UNIX Linux

Each word and/or Linux is a different length. How do strip or remove the last character from each line using bash or ksh shell only on a Linux or Unix-like systems?[donotprint]

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 2m
[/donotprint]
You can use any one of the following commands:
  • Bash/ksh shell substitution
  • cut command
  • head command
  • tail command

Bash/ksh shell substitution example

The syntax to remove last character from line or word is as follows:

x="foo bar"
echo "${x%?}"

Sample outputs:

foo ba

The % is bash parameter substitution operators which remove from shortest rear (end) pattern. You can use the bash while loop as follows:

#!/bin/bash
while IFS= read -r line
do
       echo "${line%?}"
       # or put updated line to a new file
       #echo "${line%?}" >> /tmp/newfile
done < "/path/to/file"

cut command example

The syntax is as follows:

## if length of STRING is 3, pass 2 as  character positions 
echo "foo"| cut -c 1-2
## if length of STRING is 14, pass 13 as character positions 
echo "nixCraft Linux" | cut -c 1-13
See also:

# Additional correction by James K; Editing by VG – log #


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 8 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • punktyras Apr 27, 2011 @ 14:15

    whiile IFS= read -r line

    whiile -> while

  • null May 11, 2011 @ 7:55

    Did you ever heard of “wc -l”? Or “head”/”tail” commands?

  • Keilaron Nov 19, 2011 @ 20:25

    Although a bit rude, the previous comment has a point: You can use head and tail to strip off characters. If you want it to be the first X or last X, you can use -/+, like so:

    echo '"foo!"'

    “foo!”

    echo '"foo!"' | head -c -2

    foo!”

    echo '"foo!"' | tail -c +2

    “foo!

    echo '"foo!"' | head -c -2 | tail -c +2

    foo!
    This is particularly useful with tree -Q (it doesn’t escape properly otherwise).

  • Seshadri Jun 3, 2013 @ 1:24

    Excellent, Keilaron !

    Just noticed a typo in your post (transposed cut paste):

    echo '"foo!"' | head -c -2
    “foo!

    echo '"foo!"' | tail -c +2
    foo!”

  • NightFlight Mar 19, 2014 @ 16:23

    echo ‘”foo!”‘ | head -c -2
    head: illegal byte count — -2

    • Keilaron Mar 23, 2014 @ 22:19

      Odd, I just updated and it still works for me.
      What do you get from head –version (which distro/OS, too)? I get head (GNU coreutils) 8.21 Packaged by Gentoo (8.21 (p1.0))…

      • 🐧 Nix Craft Apr 12, 2014 @ 6:33

        Mostly head/tail command on *BSD/OSX and older Unix-like oses will not accept GNU/{head|tail} syntax.

        • Keilaron Apr 16, 2014 @ 18:05

          Oh, good point. I even have an old Mac here I could have tested on, but I didn’t think of it. I was just thinking of Linux at the time.. and yeah, the GNU/Linux tools tend to have little quirks and additions.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum