$ { date; top -b -n1 | head ; } >logfile# 'date' and 'top' output are concatenated,# could be useful sometimes to hunt for a top loader )
$ { date; make 2>&1; date; } | tee logfile# now we can calculate the duration of a build from the logfile
There is a subtle syntactic difference with ( ), though (see bash reference) ; essentially, a semicolon ; after the last command within braces is a must, and the braces {, }must be surrounded by spaces.
$ time for ((i=0; i<10000000; i++)); do [[ "$i" = 1000 ]]; done
real 0m24.548suser 0m24.337ssys 0m0.036s$ time for ((i=0; i<10000000; i++)); do [ "$i" = 1000 ]; done
real 0m33.478suser 0m33.478ssys 0m0.000s
Also, brace expansions create lists of strings which are typically iterated over in loops:
$ echo f{oo,ee,a}dfood feed fad
$ mv error.log{,.OLD}(error.log is renamed to error.log.OLD because the brace expressionexpands to "mv error.log error.log.OLD")
$ for num in {000..2}; do echo "$num"; done000001002
$ echo {00..8..2}00 02 04 06 08
$ echo {D..T..4}D H L P T
~:$ echo $SHELL/bin/bash
~:$ echo ${#SHELL}9
~:$ ARRAY=(one two three)
~:$ echo ${#ARRAY}3
~:$ echo ${TEST:-test}test
~:$ echo $TEST
~:$ export TEST=a_string
~:$ echo ${TEST:-test}a_string
~:$ echo ${TEST2:-$TEST}a_string
~:$ echo $TEST2
~:$ echo ${TEST2:=$TEST}a_string
~:$ echo $TEST2a_string
~:$ export STRING="thisisaverylongname"
~:$ echo ${STRING:4}isaverylongname
~:$ echo ${STRING:6:5}avery
~:$ echo ${ARRAY[*]}one two one three one four
~:$ echo ${ARRAY[*]#one}two three four
~:$ echo ${ARRAY[*]#t}one wo one hree one four
~:$ echo ${ARRAY[*]#t*}one wo one hree one four
~:$ echo ${ARRAY[*]##t*}one one one four
~:$ echo $STRINGthisisaverylongname
~:$ echo ${STRING%name}thisisaverylong
~:$ echo ${STRING/name/string}thisisaverylongstring
if [ CONDITION ] Test constructif [[ CONDITION ]] Extended test constructArray[1]=element1 Array initialization[a-z] Range of characters within a Regular Expression$[ expression ] A non-standard & obsolete version of $(( expression )) [1]
${variable} Parameter substitution${!variable} Indirect variable reference{ command1; command2; . . . commandN; } Block of code{string1,string2,string3,...} Brace expansion{a..z} Extended brace expansion{} Text replacement, after find and xargs
括号
( command1; command2 ) Command group executed within a subshellArray=(element1 element2 element3) Array initializationresult=$(COMMAND) Command substitution, new style>(COMMAND) Process substitution<(COMMAND) Process substitution
Truncate the contents of a variable
$ var="abcde"; echo ${var%d*}abc
Make substitutions similar to sed
$ var="abcde"; echo ${var/de/12}abc12
Use a default value
$ default="hello"; unset var; echo ${var:-$default}hello
# gitit does an autosave commit to the current# if Git is installed and available.# If git is not available, it will use brew# (on macOS) to install it.## The first argument passed, if any, is used as# the commit message; otherwise the default is used.gitit() {$(exists git) && {git add --all;git commit -m "${1:-'GitBot: dev progress autosave'}";git push;} || brew install git;}