close

[轉載請註明出處] http://kezeodsnx.pixnet.net/blog

作者: kezeodsnx

在Shell script中,所有的cmd是依序執行的。有時候,可能需要某幾個cmd為一組,要嘛都run,要嘛都不run,此時,command group就派上用場了。有兩種使用方式:()和{},其差異為前者是使用subsell (nested subshell),意即會開新的shell來執行command group,在此subshell所做的改變不影響原shell。後者是在同一個shell (no-named command group),因此share相同的shell environment。

舉個例:

#!/bin/bash

test="value1"
echo "Value before command group: $test"
(
#Change value in command group
test="value2"
echo "Value in command group after change: $test"
)
echo "Value after change: $test"

exit 0
===========================================

Output (使用()):

Value before command group: value1
Value in command group after change: value2
Value after change: value1


Output (使用{}):

Value before command group: value1
Value in command group after change: value2
Value after change: value2

arrow
arrow
    全站熱搜

    kezeodsnx 發表在 痞客邦 留言(0) 人氣()