-2

I'm taking a linux course and want to know if it's possible to do something like the following:

cd (whereis cmatrix)

Since linux is slowly transforming my brain to do things quickly and efficiently, I'm sure there's a way to do this so I don't have to type out the entire directory.

Am I wrong?

Mathew
  • 318
  • 11
  • Does this answer your question? [Using the result of a command as an argument in bash?](https://stackoverflow.com/questions/58207/using-the-result-of-a-command-as-an-argument-in-bash) – Quentin Aug 31 '23 at 14:06

1 Answers1

1

You can use command substitution, but you probably meant to use which instead of whereis.

path="$(which cmatrix)"
cd "${path%/*}" # "%/*" strips the trailing filepath

or

cd "$(which cmatrix | sed 's#[^/]*$##')"

If you have dirname available, then:

cd "$(dirname "$(which cmatrix)")"

Don't forget to quote the substituted value!

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thanks, but the following is more what I need since "which cmatrix" isn't a directory: cd "$(which cmatrix | sed 's/cmatrix//')", trying to make it generic now – Mathew Aug 31 '23 at 14:14