syntactic sugar
Last updated
Was this helpful?
Last updated
Was this helpful?
[!TIP]
[!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:]
[!NOTE|label:references:]
compress and ssh and extract
tips
[!NOTE|label:references:]
for JENKINS_HOME
backup all config.xml
in JENKINS_HOME
back build history
[!TIP|label:references:]
via timestamp
[!NOTE]
[!TIP]
[!TIP|label:references:]
gz
tar.gz
tar.xz
zip
[!NOTE]
[!NOTE|label:references:]
[!NOTE|label:references:]
[!NOTE|label:references:]
more usage
<<<
, < <(..)
[!TIP]
The difference between
<(...)
and>(...)
is merely which way the redirections are done
< <(..)
&& > >(..)
[!NOTE]
example:
< <(..)
[!TIP|label:referencs:]
tips:
common usage
> >(..)
[!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.
${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"
$@
${@: 0}
${@: 1}
${@: 2}
${@: 2:1}
${@: 2:2}
${@: -2}
${@: -2:1}
${*: -1}
or ${@: $#}
${@: 1:$#-1}
sample with uncertain parameters
${@@Q}
printf " %q" "${@}"
[!NOTE]
String replacement
[!TIP]
${#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
problematic code:
correct code:
example
problematic code:
correct 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
[!NOTE]
sample:
typeset
eval \$$
more:
{!parameter}
[!NOTE]
sample
typeset
{!parameter@}
more
[!NOTE|label:references]
[!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/completions
or/etc/bash_completion.d
ubuntu:
/usr/share/bash-completion/completions
print existing completion
remove completion
[!TIP|label:references:]
$ compgen -c
commands
$ compgen -a
aliases
$ compgen -b
built-ins
$ compgen -k
keywords
$ compgen -A function
functions
$ compgen -A function -abck
all above
[!NOTE|label:references:]
to check link of bash-completion
add more completion files
more
enable
add more completion files
centos
[!TIP|label:rerefences:]
download/install
setup for specific alias
example
$ 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/ssh
or $(brew --prefix)/etc/bash_completion.d/ssh
[!TIP|label:references:]
HISTTIMEFORMAT
i.e.: $ echo |
-> esc *
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:]
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
FUNCNAME
have no effect. IfFUNCNAME
is unset, it loses its special properties, even if it is subsequently reset.This variable can be used with
BASH_LINENO
andBASH_SOURCE
. Each element ofFUNCNAME
has corresponding elements inBASH_LINENO
andBASH_SOURCE
to describe the call stack. For instance,${FUNCNAME[$i]}
was called from the file${BASH_SOURCE[$i+1]}
at line number${BASH_LINENO[$i]}
. Thecaller
builtin displays the current call stack using this information.
via
< <(
is
|
|
|
|
BASH_LINENO
An array variable whose members are the line numbers in source files where each corresponding member of FUNCNAME
was 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).
Use LINENO
to obtain the current line number.