LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-26-2011, 06:24 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Rep: Reputation: 69
Does the output of the previous command get stored in any variable...


For example, when using bash you can use
Code:
!!
to execute the previous command or
Code:
!<number>
to execute the Nth command(use history to see the list). Or you can use
Code:
cd !-2:1
to cd into the value in the first field that was executed 2 commands ago

Anyhow, say I run a command and the output is a path. Any way to cd and then some variable where OUTPUT of the previous command was stored? A variable that always stores the OUTPUT of the last command.

Last edited by trist007; 03-26-2011 at 06:31 PM.
 
Old 03-26-2011, 06:27 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
I take it you are using bash as your shell, correct? Run "echo $SHELL" and it will tell you.

As far as your command "history" (hint hint) it is stored in a file called .bash_history in your home directory...
 
Old 03-26-2011, 06:28 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
That is rather simple:
Code:
OUTPUT='your-path-delivering-command'
cd $OUTPUT
 
Old 03-26-2011, 08:03 PM   #4
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
You can also use:
Code:
cd `code that outputs path here`
Using the grave accent key (QWERTY has it above tab key)
 
Old 03-26-2011, 08:37 PM   #5
acc_kc
LQ Newbie
 
Registered: Mar 2011
Location: /usr/share/zoneinfo/Europe/Prague
Distribution: Gentoo Linux
Posts: 3

Rep: Reputation: 0
Or, if dealing with a return value, you can use $?.
 
Old 03-26-2011, 10:00 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Quote:
Originally Posted by acc_kc View Post
Or, if dealing with a return value, you can use $?.
This will only return a numerical value that indicates if the previous command was successful or returned an error-code.
 
Old 03-26-2011, 10:19 PM   #7
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 97
I read through the whole post and noticed that the original question
was not answered, namely if these is a variable that stores the
output of the last command. No, there isn't.

I would just recommend using quotes with the solution from spazticclown
in case there are spaces or special characters in the path.

Code:
cd "`command to generate path`"
 
Old 03-26-2011, 10:37 PM   #8
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Original Poster
Rep: Reputation: 69
Great thank you all for your replies.
 
Old 03-27-2011, 12:28 AM   #9
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Original Poster
Rep: Reputation: 69
Code:
cd `!!`
works well too.
 
1 members found this post helpful.
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
use output of previous command as parameter to another command raviluchmun Linux - Newbie 4 11-14-2010 01:35 AM
Output a C variable with a shell command paliga Programming 4 07-20-2010 07:10 AM
how to save output from a command to a variable? tomolesonjr Linux - Newbie 6 04-19-2006 04:04 PM
passing command output to variable Infernal211283 Linux - Newbie 12 01-11-2006 06:54 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 07:12 PM


All times are GMT -5. The time now is 06:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration