/dev/null 2>&1 이게 뭐죠?

에드워드의 이미지

보통 파일하나 만들때
cat /dev/null > a.txt
라고 하면 0byte 짜리 a.txt 파일이 생기잖아요...

근데, 뒤에 2>&1 은 뭐죠?
어떨때 사용하는 건가요?

무한포옹의 이미지

에드워드 wrote:
보통 파일하나 만들때
cat /dev/null > a.txt
라고 하면 0byte 짜리 a.txt 파일이 생기잖아요...

근데, 뒤에 2>&1 은 뭐죠?
어떨때 사용하는 건가요?

shell redirection 을 공부하시면 됩니다 . man bash

간단하게 얘기하면

1 은 standard out - 표준출력

2 는 standard error - 표준 에러

2>&1 은 표준 에러를 표준 출력으로 redirection 하는 거지요.

예를 들면

 $ echo hi > hi.txt // 1> hi.txt 에서 1을 생략해도 됩니다. 표준 출력을 hi.txt 에 저장 
$ cat hi.txt
hi
$ cat hi.txt > /dev/null  // 표준 출력을 /dev/null 로 리다이렉션합니다. 결과없음
$ rm hi.txt  // hi.txt 를 지웁니다
$ cat hi.txt  
cat: hi.txt: No such file or directory // 에러출력입니다.
$ cat hi.txt >/dev/null
cat: hi.txt: No such file or directory // 위와 같은 에러
$ cat hi.txt > /dev/null 2>&1  // 표준 에러를 표준 출력으로 redirection
$  // 아무런 출력이 없습니다.
$ rm hi.txt 
$ rm hi.txt 
rm: cannot remove `hi.txt': No such file or directory 
$ rm hi.txt > hi_err.txt 2>&1 // 위의 에러를 표준 출력으로 돌려  hi_err.txt 로 저장  
$ cat hi_err.txt 
rm: cannot remove `hi.txt': No such file or directory 
$

-------------------------------
== warning 대부분 틀린 얘기입니다 warning ===

haewoo의 이미지

Quote:
cat /dev/null > a.txt

/dev/null의 내용을 Standard out으로 출력해서 a.txt에 담는다.

Quote:
cat /dev/null > a.txt 2>&1

cat /dev/null 을 수행하면서 나타나는 Standard out의 결과를 a.txt에 담고, Standard Error의 내용도 a.txt에 담는다.

Quote:
2>&1

는 stderr의 출력을 Redirection한 내용입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.