Skip to content
Advertisement

Remove part of a string by using bash

I have the path /home/bamboo/bamboo-agent-home/xml-data/build-dir/NG-VOSGQL239-JOB1 and would like to get just /home/bamboo/bamboo-agent-home/xml-data/build-dir/ How can I delete the last part of the path if it can be with different lengths by using Bash?

Advertisement

Answer

Using bash regex =~:

$ var=/home/bamboo/bamboo-agent-home/xml-data/build-dir/NG-VOSGQL239-JOB
$ [[ $var =~ .*/ ]] && echo "${BASH_REMATCH[0]}"
/home/bamboo/bamboo-agent-home/xml-data/build-dir/
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement