COOLBEANS
COOLBEANS

Reputation: 757

Can these two bash commands be joined into one?

I've been trying to get the following two commands into one command:

var=$(find dir/* -name "$file")
var=$(basename "$var")

I thought this might work:

var=$(basename $(find dir/* -name "$file"))

I also tried pipeline, but no luck

Any thoughts?

Upvotes: 1

Views: 37

Answers (2)

user3146587
user3146587

Reputation: 4320

Use the -exec option of the command find to apply the basename command on each result:

var=$(find dir/* -name "$file" -exec basename {} ';')

Upvotes: 1

Paul
Paul

Reputation: 141829

Add another set of quotes:

var=$(basename "$(find dir/* -name "$file")")

Upvotes: 1

Related Questions