I have a variable set with var='type_cardio_10-11-2017'. I need to remove the last 10 letters from the variable, and append the remaining value to var2.

I tried with the following script, but it doesn't work as expected.

var2=${var} | cut -f1 -d "_"
echo ${var2}

The output I want is type_cardio.


To remove everything from after the last _ in var and assign the result to var2:


The parameter expansion ${parameter%word} removes the pattern word (_* in this case) from the end of the value of the given variable.

The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.

To remove the last underscore and the 10 characters after it, use


To remove characters corresponding to a date string such as the one in your example, use


Which pattern you use depends on how carefully you want to perform the match.

|improve this answer|||||

You actually want to remove the trailing 11 characters from the string; here's another way to do it:

$ var=type_cardio_10-11-2017
$ var2=${var%???????????}
$ echo "$var2"
|improve this answer|||||

Another approach in bash:

echo "${var::-10}"

Or in older versions:

echo "${var::${#var}-10}" #or
echo "${var: : -10}"
|improve this answer|||||
  • define "older versions" – qodeninja Aug 29 '19 at 18:13

1) bash solution:


2) cut solution:

var2=$(cut -d'_' -f1,2 <<<"$var1")
echo "$var2"

The output:

|improve this answer|||||
  • Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ? – Rak kundra Oct 20 '17 at 17:44
  • 1
    @SanthoshPogaku, that was not clear from your initial question, you have my update – RomanPerekhrest Oct 20 '17 at 17:52
  • Yeah sir, I got what am i looking for . Thank's for your valuable time. – Rak kundra Oct 20 '17 at 17:53

Appending var and var2 is the easy part - you can just join them like new_var="$var$var2". If what you really meant to say is you want to store cropped var into var2,then it's just var2=$(...) and you can put the commands that other users and my answer presented here inside the $() portion.

The main part is removing those 10 characters. There's number of ways to extract the part you want from var:

  • printf (pretty portable, doesn't rely on specific shell)

    $ printf "%.11s\n"  "$var"                                                                                                                                                                      
  • awk

    $ var=type_cardio_10-11-2017                                                                                                                                                                    
    $ awk -v awk_var="$var"  'BEGIN{print substr(awk_var,0,length(awk_var)-11)}'                                                                                                                    

    or you can use printf trick here as well:

    $ echo "$var" | awk '{printf "%.11s\n",$0}'                                                                                                                                                     
  • egrep

    $ echo "$var" | egrep  -o '^.{0,11}'                                                                                                                                                           
  • perl:

    $ echo "$var" | perl -lne 'print substr($_,0,11)'                                                                                                                                               
  • python:

    $ python -c 'import sys;print sys.argv[1][0:11] ' "$var"                                                                                                                                        
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.