0

I have strings that I want to extract to a variable the part after the last / character.

Sample strings are:

apps/test/wordpress/wordpressA
apps/wordpress/wordpressA
apps/wordpressA
wordpressA

I want to create a new_variable with just wordpressA in it from all the samples. I won't know how many / characters there may be in each variable.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
NasKar
  • 59
  • 5
  • This is very similar to [Expression after last specific character](https://stackoverflow.com/q/9532654/3266847) – Benjamin W. Jan 06 '21 at 01:25
  • Or [Get the characters after the last index of a substring from a string](https://stackoverflow.com/q/15548277/3266847) – Benjamin W. Jan 06 '21 at 01:25
  • Note that I strongly recommend using [@chepner's answer](https://stackoverflow.com/a/15548660/14122) rather than the accepted one. – Charles Duffy Jan 06 '21 at 01:27
  • ...btw, this is also covered as part of [BashFAQ #100](https://mywiki.wooledge.org/BashFAQ/100#Removing_part_of_a_string). – Charles Duffy Jan 06 '21 at 01:28
  • This particular case is completely covered with `basename` so one does not need to use any regexps thus making everything simpler to understand and maintain. – miwa Jan 06 '21 at 01:29

1 Answers1

1

Easy

Each echo below will output wordpressA

strInput='apps/test/wordpress/wordpressA'   
echo "${strInput##*/}"   
   
strInput='apps/wordpress/wordpressA'   
echo "${strInput##*/}"   
   
strInput='apps/wordpressA'   
echo "${strInput##*/}"   
   
strInput='wordpressA'   
echo "${strInput##*/}"   
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
svin83
  • 239
  • 1
  • 4
  • 13