Ali Shahbaz
Ali Shahbaz

Reputation: 81

How to extract folder name as a variable in bash

I have directory

MainProject/src/

My script which I am calling test.sh run at /MainProject/ and here is some part of the sript:

dotnet restore src/*.sln
dotnet msbuild -t:publish src/*.sln -p:Configuration=Release

For this command, I want MainProject.Test as variable VAR:

dotnet vstest VAR/bin/Release/netcoreapp1.1/VAR.dll

or something like this:

dotnet vstest {src/*.Test}/bin/Release/netcoreapp1.1/{src/*Test}.dll

Which contains these files and folders:

Files:

project.sln

somescript.sh

Folders:

MainProject.Test

MainProject.Host

What I need to do is fetch MainProject.Test folder name and set it to a variable, but I also need it to be templatized where I can set it to a variable using something like *.Test

The reason for this is that I need the script parametrized because there are

MainProject2

MainProject3

MainProjectx

using the same naming convention.

Upvotes: 2

Views: 5778

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295272

The current directory is in $PWD. That's fully-qualified; to remove everything from the beginning up to the last / would be ${PWD##*/}, using a parameter expansion.

Thus, to extract the last piece of the current working directory and add .Test as a suffix:

result=${PWD##*/}.Test

Upvotes: 3

Related Questions