LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cd `dirname `which prog`` (https://www.linuxquestions.org/questions/linux-general-1/cd-%60dirname-%60which-prog%60%60-878281/)

mzh 05-02-2011 04:04 AM

cd `dirname `which prog``
 
Hey guys,
I'm trying to remember how to use the output of a nested Bash builtin call.
So `which prog` gives me the path to the program, i'm interested in.
Then I would like to get the directory path leading to that program and plug it into 'cd', so i end up in the directory containing the program.

What am I missing?

thanks for any hints.

colucix 05-02-2011 04:10 AM

The syntax for command substitution that uses back ticks, does not permit nesting. Use the $(command) syntax instead:
Code:

cd $(dirname $(which prog))
Hope this helps.

mzh 05-02-2011 05:36 AM

Quote:

Originally Posted by colucix (Post 4343660)
The syntax for command substitution that uses back ticks, does not permit nesting. Use the $(command) syntax instead:
Code:

cd $(dirname $(which prog))
Hope this helps.

+1 for this.
Thanks.

MTK358 05-02-2011 10:26 AM

Quote:

Originally Posted by colucix (Post 4343660)
The syntax for command substitution that uses back ticks, does not permit nesting.

It actually does, but the syntax is really ugly. But I also strongly recommend $() over backticks.

Telengard 05-02-2011 10:46 AM

Quote:

Originally Posted by MTK358 (Post 4343976)
It actually does, but the syntax is really ugly. But I also strongly recommend $() over backticks.

For those like me who have to know.

Code:

word_count=` wc -w \`echo * | awk '{print $8}'\` `
http://www.tldp.org/LDP/abs/html/com...ml#FTN.AEN7219

David the H. 05-02-2011 10:52 AM

The Why is $(...) preferred over `...` (backticks)? FAQ page details the differences between the two forms, including complications with nesting, escaping, and quoting.


All times are GMT -5. The time now is 05:54 PM.