Skip to content
Advertisement

How to get current foldername and remove characters from the name in bash

I’m trying to write a single command in my makefile to get the current folder and remove all “.” from the name. I can get the current folder with $${PWD##*/} and I can remove the “.”s with $${PWD//.} but I can’t figure out how to combine these two into one.

The reason I need this is to kill my docker containers based on name of project. This is my command:

docker ps -q --filter name="mycontainer" | xargs -r docker stop

and i was hoping I could inject the project name before my container name like this:

docker ps -q --filter name=$${PWD##*/}"_mycontainer" | xargs -r docker stop

Advertisement

Answer

You can try:

var=$(echo ${PWD##*/} | sed "s/.//")

or:

var=$(tmp=${PWD##*/} && printf "${tmp//./}")

In your use case will be something like:

docker ps -q --filter name=$(tmp=${PWD##*/} && printf "%s_mycontainer" "${tmp//./}") | xargs -r docker stop

Note that there are more ways to do that (even more efficient).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement