LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 05-02-2011, 05:04 AM   #1
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Rep: Reputation: 0
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.
 
Old 05-02-2011, 05:10 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 members found this post helpful.
Old 05-02-2011, 06:36 AM   #3
mzh
Member
 
Registered: Apr 2011
Location: Copenhagen
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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.
 
Old 05-02-2011, 11:26 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by colucix View Post
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.
 
1 members found this post helpful.
Old 05-02-2011, 11:46 AM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by MTK358 View Post
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
 
Old 05-02-2011, 11:52 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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


Reply

Tags
backticks, bash, command line, command substitution


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc complains about files names been the same as dirname nano2 Linux - Software 5 06-23-2008 05:32 AM
How to get the dirname only from pwd? EmLS Linux - General 3 04-21-2006 02:22 AM
Nautilus and dirname drgilberto Programming 0 02-14-2006 03:08 PM
I want to start a prog from another prog but not as child grupoapunte Programming 5 05-23-2005 06:37 PM
dirname error when installing Java JRE jrdioko Linux - Software 0 05-20-2005 06:35 PM


All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration