Thursday, October 28, 2021

[SOLVED] Remove last two strings of a variable in shell

Issue

i have a variable var=/usr/local/bin/test/exec

now i have to remove last 2 strings in the above variable say:

var=/usr/local/bin/

and after removing the last 2 strings i have to use this variable 'var' in a shell loop.

tried :

  var='/usr/local/bin/test/exec'
  echo ${var#$(dirname "$(dirname "$s")")/}

output:

test/exec

am getting the truncated part as output, but am expecting the rest of the part not the truncated part

Any suggetions


Solution

OK after some googling i have found the solution for this:

var1="$(echo $var | cut -d '/' -f-4)"


Answered By - Rczone