LinuxQuestions.org
Review your favorite Linux distribution.
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 01-22-2014, 10:41 AM   #1
ashley75
Member
 
Registered: Aug 2003
Posts: 232

Rep: Reputation: 30
syntax question


From time to time I see the syntax below and I wonder what exactly it's doing?

"2>&1 &" at the end of the command.

Thanks,
 
Old 01-22-2014, 11:02 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,502

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by ashley75 View Post
From time to time I see the syntax below and I wonder what exactly it's doing?

"2>&1 &" at the end of the command.

Thanks,
Google answers all questions. Do a Google search on 2>&1 & and gets lots of hits.

Daniel B. Martin
 
Old 01-22-2014, 11:25 AM   #3
Rawcous
Member
 
Registered: Jan 2014
Location: Farnborough, Hampshire - UK
Distribution: Previously 2 * Fedora 18 Servers -> 2 * CentOS 6.7 + random distro testing
Posts: 96

Rep: Reputation: Disabled
Hey,

DanielBMartin is correct - Google should be your first port of call as it shows you have at least tried to research the question yourself. The long and short of it is that it sends all output from a command that would typically be echoed to the screen to the same file as well as any error output. i.e.

find / testfile > listing 2>&1 will send all output to a file called listing This file will also include all error output.

find / testfile > listing 2> listing2 will send the output of the find command to a file called listing, but any error output i.e. permission denied messages will be redirected to a second file called listing2

Adding the ampersand & at the end simply places the command in the background allowing you to continue using the current terminal session whilst the command is running - beware that if you quit the terminal session while the process is running (i.e. ctrl-d,exit..) the background command will be terminated.

If you wish the background command to continue even if you do quit the current terminal session you need to do the following:

nohup this-is-your-command &

Remember Google is your friend first and foremost...

Regards,

Rawcous!!!
 
1 members found this post helpful.
Old 01-22-2014, 11:26 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,349

Rep: Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910
Quote:
Originally Posted by ashley75 View Post
From time to time I see the syntax below and I wonder what exactly it's doing?

"2>&1 &" at the end of the command.
This has been a common question.
  • 1 represents standard out (stdout) and 2 represents standard error (stderr).
  • 2>&1 combines the output of 2 to 1 and vice versa.
  • The trailing "&" backgrounds it
So, a common usage would be for cron jobs, to redirect errors/outputs of running scripts. The trailing "&" in that case would start a new background process for that script.
 
Old 01-23-2014, 02:32 AM   #5
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
For more info, here a good resource.
 
  


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
If...then...else syntax question kaplan71 Linux - Software 8 10-14-2009 11:32 PM
Question of syntax kaplan71 Linux - Software 1 05-05-2009 03:45 PM
question about C++ syntax bx.s Programming 3 07-27-2006 08:50 PM
C++ syntax question jiml8 Programming 6 06-20-2006 05:19 PM
Syntax question satimis Linux - Newbie 9 09-23-2004 08:47 AM


All times are GMT -5. The time now is 06:25 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