LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 04: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, 04:10 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 05: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, 10:26 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 10:46 AM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
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, 10:52 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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 04:32 AM
How to get the dirname only from pwd? EmLS Linux - General 3 04-21-2006 01:22 AM
Nautilus and dirname drgilberto Programming 0 02-14-2006 02:08 PM
I want to start a prog from another prog but not as child grupoapunte Programming 5 05-23-2005 05:37 PM
dirname error when installing Java JRE jrdioko Linux - Software 0 05-20-2005 05:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:49 AM.

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
Open Source Consulting | Domain Registration