user3927312
user3927312

Reputation: 824

Find directory of a command in bash

How do I find the directory of a command in bash? I'm not looking which

For example which cat tells me /bin/cat. I'm looking for something which only gives me /bin

Upvotes: 0

Views: 61

Answers (2)

iBug
iBug

Reputation: 37237

You can also use bash builtin string substitution:

CAT=$(which cat)
echo "${CAT%/*}"

Output:

/bin

Upvotes: 1

Matt
Matt

Reputation: 3760

Use dirname /bin/cat

/bin

Similarly you can use basename /bin/cat

cat

Upvotes: 4

Related Questions