LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-23-2015, 02:05 AM   #1
rockstar05
Member
 
Registered: Dec 2011
Posts: 83

Rep: Reputation: Disabled
special variable in Linux that stores the last command output?


Is there a special variable in Linux that stores the last command output?

Kindly let me know if there is any?
 
Old 06-23-2015, 02:59 AM   #2
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
special variable in Linux that stores the last command output?

which output to be clear. stdout, stderr or the exit code
 
Old 06-23-2015, 03:00 AM   #3
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
special variable in Linux that stores the last command output?

also what shell script language are you using
bash dash sh another?
 
Old 06-23-2015, 03:04 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
If using bash, and if you mean the exit status of the last command, it is $?.

See more info here.
 
Old 06-23-2015, 03:18 AM   #5
rockstar05
Member
 
Registered: Dec 2011
Posts: 83

Original Poster
Rep: Reputation: Disabled
Not the Exit status, I want to know if there is any variable that stores the output of previous Linux command.

I am using bash scripting Language.

Thanks.
 
Old 06-23-2015, 03:23 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
No.
 
Old 06-23-2015, 03:36 AM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
No but you can use '>' to redirect to a file or use the 'tee' command to redirect to a file and to stdout, you can also use command substitution like so
Code:
keithhedger@LFSCerebro:~-> x=$(ls)
keithhedger@LFSCerebro:~-> echo $x
backdrops bashrc cdtdebugger conky ConkyThemes Desktop Desktop-1 Desktop.BAK Desktop.bak2 Documents Downloads Dropbox Dropbox.ini Dump examples googledirectlink kkedit-0.2.2-1.1.x86_64.cpio kkeditdebug lddgrep LFSPkgBuildScripts lfssession.pid Maildir Necronomicon.swf noip-duc-linux noip-duc-linux.tar.gz osx.vnc Patterns Projects PythonScripts quarantine Scripts SpaceFMScripts startnewx SVN temails Templates testhomenet.grsync VBox VBShared Wallpapers workspace-cdtdebug xfce4-meta.LFSBuild.BAK xinits

Last edited by Keith Hedger; 06-23-2015 at 03:37 AM. Reason: missed a bit
 
Old 06-23-2015, 08:29 AM   #8
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
To check for yourself, type this in the terminal and hit Alt+Shift+*

Code:
echo $
It (should) auto-complete onto the terminal all of your variables in your environment. Then by hitting enter you can see what each variable holds (empty ones will show nothing)
 
Old 06-23-2015, 08:36 AM   #9
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
TAB is the standard auto complete key, if you want to see all variable use 'env'
 
Old 06-23-2015, 08:39 AM   #10
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
TAB lists all options and will let you autocomplete one into the terminal (with usage of * to sometimes act upon multiple options).
Alt-* causes bash to take all of those available options and put them into the command line - which can then be acted upon.
Different keys for different purposes.

However, I was not aware of env - thank you.

Code:
       insert-completions (M-*)
              Insert all completions of the text before point that would have been generated by possible-com
              pletions.
       complete (TAB)
              Attempt to perform completion on the text before point.  Bash attempts completion treating  the
              text  as a variable (if the text begins with $), username (if the text begins with ~), hostname
              (if the text begins with @), or command (including aliases and functions) in turn.  If none  of
              these produces a match, filename completion is attempted.

Last edited by Sefyir; 06-23-2015 at 08:41 AM.
 
Old 06-23-2015, 09:11 AM   #11
linom
Member
 
Registered: May 2015
Location: India
Distribution: Debian, CentOS,Redhat, Fedora, Ubuntu
Posts: 91

Rep: Reputation: 13
In bash, you can try:
~# echo $_
But, there also has some limitation. A command with parameters / arguments, shall return the last part.
For eg. if the last command is
~# ls -l
the command ~# echo $_ will return only the second part, i.e. "-l"

Last edited by linom; 06-23-2015 at 09:12 AM.
 
  


Reply



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
redirecting a Command output to a Variable Ahmadhr Linux - Software 2 11-05-2013 04:31 AM
save command output to variable rajachan Programming 4 01-18-2013 07:48 AM
[SOLVED] Is it the PATH variable that stores the paths of all the linux commands? abhishekgit Linux - Newbie 5 08-17-2012 10:00 AM
how to save output from a command to a variable? tomolesonjr Linux - Newbie 6 04-19-2006 03:04 PM
passing command output to variable Infernal211283 Linux - Newbie 12 01-11-2006 05:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:49 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
Open Source Consulting | Domain Registration