how to chop last n bytes of a string in bash string choping?

bash

Solution

If your string is stored in a variable called $str, then this will get you give you the substring without the last 20 digits in bash

${str:0:${#str} - 20}

basically, string slicing can be done using

${[variableName]:[startIndex]:[length]}

and the length of a string is

${#[variableName]}

EDIT: solution using sed that works on files:

sed 's/.\{20\}$//' < inputFile

Problem

for example `qa_sharutils-2009-04-22-15-20-39`, want chop last 20 bytes, and get '`qa_sharutils`'. I know how to do it in sed, but why `$A=${A/.\{20\}$/}` does not work? Thanks!

Original source

Related problems