Appending with 2> and with >>


Appending with 2> and with >>



Why do some scripts use 2> instead of >>?
Is the 2 just a multiplier of the >'s ?


2>


>>


>





Read the Bash manual page
– Some programmer dude
Jun 29 at 10:45




3 Answers
3



1>> and 2>> are redirections for specific file-descriptors, in this case the standard output (file descriptor 1) and standard error (file descriptor 2).
2> Will output the error in the script to a file.
">>" Will append the output of a command to an existing file. If file doesn`t exists then it will create the file add the output to the file.



See this link => https://unix.stackexchange.com/questions/183125/what-does-1-and-2-mean-in-a-bash-script



For 2> means you are re-directing standard errors to a file or etc.


2>



>> means we are trying to do concatenating operation.


>>



Examples:



Without 2> :


2>


ls -ld singh_test
ls: cannot access singh_test: No such file or directory



With 2>:


2>


ls -ld singh_test 2>/dev/null





Ah, I see. Can I also append the standard errors with 2>>?
– thraizz
Jun 29 at 10:42


2>>





Yes, you can append using >>.
– Himanshu
Jun 29 at 10:48





@thraizz, no need fr single command if you are creating a new file then it will write in single file only by appending it by 2> itself but if file is already created one then then you could use 2>>
– RavinderSingh13
Jun 29 at 10:48



2>


2>>





@thraizz, you could see this link also what one should do once someone replies on SO stackoverflow.com/help/someone-answers
– RavinderSingh13
Jun 29 at 11:01



I/O redirection in bash is not pretty, but is very flexible.



I/O operates as "streams". The standard input stream is 0. Standard output is 1. Error output is 2.



echo foo send the literal string "foo" to the commands standard output stream 1. You can redirect this like so: echo foo 1>x which will send the data on stream 1 to a file named x. For output redirections of this sort, standard output is the default, so you will usually see people leave the 1 off, like this: echo foo >x


echo foo


echo foo 1>x


echo foo >x



This means you can specify the redirections of the standard output and error output of a command separately.


ls -ld . bogus >ok 2>err



This directs the successful listing into ok and the error message about bogus not existing into err.


ok


bogus


err



By default, > creates a file if one does not exist, but silently truncates one that already does. >> will also create a file if it does not exist, but will append to existing data rather than truncating . Thus,


>


>>


ls -ld . bogus >ok 2>>err



Will create either or both, but any previous content in ok will be lost, while err will add a new line on the end of the file.


ok


err



If you don't want that behavior, then set -o noclobber will change it so that > will create a new file, but will fail if the file already exists. You can explicitly tell it to truncate any existing file under noclobber with >| for cases where you still want that.


set -o noclobber


>


noclobber


>|



You can also close streams with (for example) 2>&- and create new duplicates with 1>&3.


2>&-


1>&3



See here for a more complete reference.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV