syntactic sugar

oneline commands

[!TIP]

cat and EOF

[!NOTE|label:references:]

while read from input

[!NOTE|label:references:]

ssh

find and tar

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:]

[!NOTE|label:references:]

using string as variable name

[!NOTE|label:references:]

<<<, < <(..)

[!TIP]

  • < <( is Process Substitution

    • The difference between <(...) and >(...) is merely which way the redirections are done

< <(..) && > >(..)

[!NOTE]

  • < <(..)

    [!TIP|label:referencs:]

    • common usage

  • > >(..)

    [!TIP]

    • Process Substitution

      • >(...) is used less frequently; the most common situation is in conjunction with tee(1).

      • >(...) is handy when redirecting the output to multiple files, based on some criteria.

parameter substitution

EXPR
DESCRIPTION

${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

EXPR
DESCRIPTION

$@

${@: 0}

${@: 1}

${@: 2}

${@: 2:1}

${@: 2:2}

${@: -2}

${@: -2:1}

${*: -1} or ${@: $#}

${@: 1:$#-1}

  • sample with uncertain parameters

quotas

string manipulations

EXPR
DESCRIPTION

${#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:

ESCAPE CODE
LANGUAGE
DESCRIPTION

\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]

  • typeset

  • eval \$$

    • more:

  • {!parameter}

echo var name

[!NOTE]

  • typeset

  • {!parameter@}

  • more

ls

[!NOTE|label:references]

bash completion

[!NOTE|label:references]

  • print existing completion

  • remove completion

  • list all completions

    [!TIP|label:references:]

    COMMAND
    DESCRIPTION

    $ compgen -c

    commands

    $ compgen -a

    aliases

    $ compgen -b

    built-ins

    $ compgen -k

    keywords

    $ compgen -A function

    functions

    $ compgen -A function -abck

    all 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

tricky

alias for sudo

[!TIP|label:references:]

get md5sum

env

shortcuts

man

  • making xtrace more useful

    [!TIP]

    • 5.2 Bash Variables - PS4

      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 ‘+ ’.

  • LINENO and BASH_LINENO

    [!NOTE|label:references:]

    • Bash VariablesBASH_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.

      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. If FUNCNAME is unset, it loses its special properties, even if it is subsequently reset.

      This variable can be used with BASH_LINENO and BASH_SOURCE. Each element of FUNCNAME has corresponding elements in BASH_LINENO and BASH_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]}. The caller builtin displays the current call stack using this information.

Last updated

Was this helpful?