syntactic sugar
oneline commands
[!TIP]
cat and EOF
[!NOTE|label:references:]
using heredoc with ssh
kubectl apply from stdin
[!NOTE|label:references:]
git apply from stdin
git apply from clipboard
[!NOTE|label:references:]
patch from stdin
[!NOTE|label:references:]
while read from input
[!NOTE|label:references:]
ssh
compress and ssh and extract
tips
[!NOTE|label:references:]
find and tar
for JENKINS_HOME
backup all
config.xmlin JENKINS_HOMEback build history
find and rename
find and sort
find and copy
[!TIP]
download and extract
[!TIP|label:references:]
gz
tar.gz
tar.xz
zip
kubectl apply from stdin
sync mirror
[!NOTE]
get all declare
[!NOTE|label:references:]
print env
[!NOTE|label:references:]
using string as variable name
[!NOTE|label:references:]
more usage

using string as var name
<<<, < <(..)
<<<, < <(..)[!TIP]
< <(is Process Substitution
The difference between
<(...)and>(...)is merely which way the redirections are done
< <(..) && > >(..)
< <(..) && > >(..)[!NOTE]
example:
> >(..)[!TIP]
>(...)is used less frequently; the most common situation is in conjunction withtee(1).>(...)is handy when redirecting the output to multiple files, based on some criteria.
parameter substitution
${variable-default}
if variable is unset, use default
${variable=default}
if variable is unset, set variable to default
${variable+alt}
if variable is set, use alt, else use null string
${variable:-default}
with ":[-=+]", condition takes also "declared but null"
arguments substitution
$@
${@: 0}
${@: 1}
${@: 2}
${@: 2:1}
${@: 2:2}
${@: -2}
${@: -2:1}
${*: -1} or ${@: $#}
${@: 1:$#-1}
sample with uncertain parameters
quotas
${@@Q}printf " %q" "${@}"[!NOTE]
String replacement
[!TIP]
string manipulations
${#string}
length
${string:position}
substring, or positional parameter with $* and $#
${string:position:length}
substring
${string#substring}
deletes shortest match of $substring from front of $string
${string##substring}
same but longest match
${string%substring}
shortest from back
${string%%substring}
longest from back
${string/substring/replacement}
replace first match
${string//substring/replacement}
replace all matches
${string/#substring/replacement}
replace if matches front end of $string
${string/%substring/replacement}
replace if matches back end of $string
${var^}
uppercase first char
${var^^}
uppercase all chars
${var,}
lowercase first char
${var,,}
lowercase all chars
compound comparison
problematic code:
correct code:
example
problematic code:
correct code:
escape code
[!TIP] references:
\x1b
Node.js
hex char
\x1b
Node.js w/ TS
hex char
\u001b
Python
hex char
\033
GNU Cpp
octal char
\033
ANSI C
octal char
\033
POSIX-compliant shells
octal char
\e
Bash
-
\c[
-
control char
echo
echo var name from variable
[!NOTE]
sample:
typeseteval \$$more:
{!parameter}
echo var name
[!NOTE]
sample
typeset{!parameter@}more
ls
[!NOTE|label:references]
bash completion
[!NOTE|label:references]
create bash completion
paths:
osx:
$(brew --prefix)/etc/bash_completion.d
completion files in
bash-completion@2:$(brew --prefix bash-completion@2)/share/bash-completion/completions/centos:
/usr/share/bash-completion/completionsor/etc/bash_completion.dubuntu:
/usr/share/bash-completion/completions
print existing completion
remove completion
[!TIP|label:references:]
COMMANDDESCRIPTION$ compgen -ccommands
$ compgen -aaliases
$ compgen -bbuilt-ins
$ compgen -kkeywords
$ compgen -A functionfunctions
$ compgen -A function -abckall above
osx
[!NOTE|label:references:]
to check link of bash-completion
add more completion files
more
linux
enable
add more completion files
centos
completion for alias
[!TIP|label:rerefences:]
download/install
setup for specific alias
example
troubleshooting
$ ssh bash_completion: _comp_compgen_known_hosts__impl: -F: an empty filename is specified[!NOTE|label:references:]
clear completion
or add into
/usr/local/etc/bash_completion.d/sshor$(brew --prefix)/etc/bash_completion.d/ssh
tricky
alias for sudo
[!TIP|label:references:]
get md5sum
env
HISTTIMEFORMAT
shortcuts
inserts the results of an autocompletion in the command line
i.e.:
$ echo |-> esc *
man
show ascii
[!TIP]
PS4 The value of this parameter is expanded like PS1 and the expanded value is the prompt printed before the command line is echoed when the -x option is set (see The Set Builtin). The first character of the expanded value is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is ‘+ ’.
[!NOTE|label:references:]
Bash VariablesBASH_LINENO An array variable whose members are the line numbers in source files where each corresponding member of
FUNCNAMEwas invoked.${BASH_LINENO[$i]}is the line number in the source file (${BASH_SOURCE[$i+1]}) where${FUNCNAME[$i]}was called (or${BASH_LINENO[$i-1]}if referenced within another shell function). UseLINENOto obtain the current line number.FUNCNAME An array variable containing the names of all shell functions currently in the execution call stack. The element with index 0 is the name of any currently-executing shell function. The bottom-most element (the one with the highest index) is "main". This variable exists only when a shell function is executing. Assignments to
FUNCNAMEhave no effect. IfFUNCNAMEis unset, it loses its special properties, even if it is subsequently reset.This variable can be used with
BASH_LINENOandBASH_SOURCE. Each element ofFUNCNAMEhas corresponding elements inBASH_LINENOandBASH_SOURCEto describe the call stack. For instance,${FUNCNAME[$i]}was called from the file${BASH_SOURCE[$i+1]}at line number${BASH_LINENO[$i]}. Thecallerbuiltin displays the current call stack using this information.
Last updated
Was this helpful?