Zaid
Zaid

Reputation: 37156

How can I use Bash to perform a substring operation on backtick output in one line?

I'm trying to get a substring of my parent directory. What's annoying is that I have to do it using two commands when one should suffice:

curDir=`pwd`; echo ${curDir##*/};

When I try to combine the two into one I get an error:

echo ${`pwd`##*/}
-bash: ${`pwd`##/}: bad substitution

How can I avoid use of a temp here?

Upvotes: 2

Views: 242

Answers (2)

Jürgen Hötzel
Jürgen Hötzel

Reputation: 19757

echo ${`pwd`##*/}

will result in a parameter expansion like

   echo ${/home/yourdir##*/}

But you don't need command substitution in you case. Bash provides a builtin variable:

echo ${PWD##*/}

Upvotes: 3

psmears
psmears

Reputation: 28120

In this instance I would do:

basename `pwd`

In the general case there may not be an equivalent for basename but you can use sed:

pwd | sed 's,.*/,,'

Upvotes: 1

Related Questions