My Jenkins environment variable $SVN_URL is http://project/svn/neslrepo/trunk/java_project. I want to extract java_project and store in a variable through my shell script.

I tried:

job_name=(echo $SVN_URL | awk -F "/" '{print $NF}')
echo $job_name.war

I expected the output to be java_project.war, but it didn't work. What am I doing wrong?


The shell already provides some nice tools to do this, no need for an external command:

$ SVN_URL="http://project/svn/neslrepo/trunk/java_project"
$ echo ${SVN_URL##*/} 

So all you need is:


It appears that basename can also do this, despite the argument being an HTTP URL instead of a filesystem path.

echo $(basename "http://project/svn/neslrepo/trunk/java_project/").war

Since the man page does not mention HTTP URLs, this is probably an abuse of basename. It seems to be a side effect of the structural similarity between an HTTP URL and a filesystem path. man 3 basename says (emphasis mine):

The functions dirname() and basename() break a null-terminated pathname string into directory and filename components. In the usual case, dirname() returns the string up to, but not including, the final '/', and basename() returns the component following the final '/'. Trailing '/' characters are not counted as part of the pathname.


I tried by below 2 methods and it worked fine


@praveen_linux_example ~]# SVN_URL=http://project/svn/neslrepo/trunk/java_project
[root@praveen_linux_example ~]# jobname=`echo $SVN_URL| awk -F "/" '{print $NF}'`
[root@praveen_linux_example ~]# echo $jobname.war
Method 2

@praveen_linux_example ~]# echo $SVN_URL| sed "s/.*\///"| sed "s/$/.war/"

The problem is in your job_name=(echo $SVN_URL | awk -F "/" '{print $NF}') line.

You are trying to use command substitution, however, you've forgotten the $.

It should be the following in order to make bash execute the commands in brackets:

job_name=$(echo $SVN_URL | awk -F "/" '{print $NF}')

The following snippet worked perfectly for me:



job_name=$(echo $SVN_URL | awk -F '/' '{print $NF}')
echo $job_name.war

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.