Shell Script#

date:

2020-12-26

version:

1

String#

Get length:

${#str}

Substring / Slice:

${str:1:4}

Replace:

${v%.md}.rst

Heredoc#

Syntax:

cat <<EOF
... Heredoc content
EOF

备注

Shell variables in heredoc will be expanded

Avoid shell expanding variables:

cat <<'EOF'
... Heredoc ${content}
EOF

Write to file:

cat << EOF > /tmp/foo
... Heredoc content
EOF

Output to variable:

read -r -d '' VAR << EOF
... Heredoc content
EOF

Command Line Arguments#

Number of pass-in arguments:

$#

The "pass-in argument list:

$* # Default, an *array*
$@ # Default, a *string*

The arguments of previous command:

!^      # First one
!$      # Last one
!*      # All
!:n     # N-th
!:n-m   # Range n~m

Command Line Wrapper 模版#

#!/bin/bash

EXEC="xxxx"

exec $EXEC $*

Bash set options#

Get help:

$ bash -c 'help set'

脚本常用参数:

-e:

Exit immediately if a command exits with a non-zero status.

-x:

Print commands and their arguments as they are executed.

-u:

Treat unset variables as an error when substituting.

评论

如果你有任何意见,请在此评论。 如果你留下了电子邮箱,我可能会通过 回复你。