[완료]쉘 스크립트에서 () 와 {} 의 차이

dcmru의 이미지

아래는 solaris 5.8의 man sh 중에서 발췌했습니다.

sub shell 과 current shell 의 차이점이 뭔지요?

(list)
Execute list in a sub-shell.

{ list;}
list is executed in the current (that is, parent)
shell. The { must be followed by a space.

list 부분에 명령어를 삽입하여 실행하여, ptree 명령어로 보니

둘다 자식 프로세스로 생성되어 실행되었습니다.

정확하게 sub-shell 이 무엇을 의미하는 것인지요?

sub shell 이 자식프로세스로 실행되는 것이 아닌가요?

현재의 쉘과 같은 이름으로 자식 프로세스가 생성되는 것인가요?

개념적으로는 이해를 하겠는데, 어떤 차이점이 있는지 모르겠습니다.

chronon의 이미지

$ AA=1
$ echo $AA
1
$ ( AA=2; )
$ echo $AA
1
$ { AA=3; }
$ echo $AA
3
dcmru의 이미지

그러니깐 현재 쉘에 영향을 미치냐 안미치냐의 차이점 같은데요.

이 것이 내부적으로 어떻게 틀린가요?

sub shell 로 실행된다면, 또다른 쉘로 포크되어 실행이 된다는 말인가요?

----------------
노력만이 살길이다.

노력만이 살길이다.

eungkyu의 이미지

네, 맞습니다. :)

dcmru의 이미지

제가 한 방법은 스크립트 파일이름을 test.sh 로 하고

#!/bin/sh
(sleep 100)
exit 0

ptree 명령어로 확인한 결과
사용자쉘
- sh
-- sleep 100

이렇게 나왔고,
다시 스크립트를 다음과 같이 수정하여

#!/bin/sh
{ sleep 100;}
exit 0

ptree 명령어로 확인한 결과 위의 결과와 같았습니다.
다른 방법으로 확인할 수 있는 방법이 있을까요?

리눅스에서 cd 명령어로 확인한 결과 예상했던대로 나오네요. ^^;
솔라리스는 내일 확인해봐야겠습니다.
----------------
노력만이 살길이다.

노력만이 살길이다.

eungkyu의 이미지

( ) 사이에 명령이 하나만 있을 때에는 그 명령이 shell built-in 명령일 때만
의미가 있습니다.

예를 들어서

$ ( cd dir )
$ cd dir

이 두가지는 분명 차이가 있지만

$ ( sleep 100 )
$ sleep 100

이 두 명령은 차이가 없습니다. 그래서 쉘에서 불필요하게 쉘 프로세스를 또 띄우지 않기 때문에 확인할 수 없는 것이구요.

( ) 사이에 (shell interpretation이 필요하도록) 명령이 여러개 들어가면 실제로 fork되어서 ps 명령으로 확인해볼 수 있습니다.

$ ps
$ ( ps )
$ { ps; }
$ ps; ps
$ ( ps; ps )
$ { ps; ps; }

이렇게 위의 명령을 실행해보면 간단히 확인할 수 있습니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.