Bash scripting cheatsheet (Italiano)

Guida introduttiva


Questo è un rapido riferimento per iniziare con Bash scripting.

  • Imparare bash in y minuti (
  • Guida Bash (mywiki.wooledge.,td> ${FOO:(-3):3} Substring from the right


    Expression Description
    ${#FOO} Length of $FOO


    Default values

    Omitting the : removes the (non)nullity checks, e.,g. ${FOO-val} expands to val if unset otherwise $FOO.,/div>

    Expression Description $# Number of arguments $* All arguments [email protected] All arguments, starting from first $1 First argument $_ Last argument of the previous command

    See Special parameters.,043f44″>

File conditions


# Stringif ]; then echo "String is empty"elif ]; then echo "String is not empty"else echo "This never happens"fi
# Combinationsif ]; then
# Equalif ]
# Regexif ]
if (( $a < $b )); then echo "$a is smaller than $b"fi
if ]; then echo "file exists"fi


Defining arrays

Fruits=('Apple' 'Banana' 'Orange')

Working with arrays



for i in "${arrayName}"; do echo $idone



declare -A sounds

Declares sound as a Dictionary object (aka associative array).,

Lavorare con i dizionari


Scorrere i valori

for val in "${sounds}"; do echo $valdone

Scorrere i tasti

for key in "${!sounds}"; do echo $keydone



set -o noclobber # Avoid overlay files (echo "hi" > foo)set -o errexit # Used to exit upon error, avoiding cascading errorsset -o pipefail # Unveils hidden failuresset -o nounset # Exposes unset variables

Glob opzioni

GLOBIGNORE come separati da due punti, elenco di motivi per essere rimosso dal globmatches.,> Expand all parameters of most recent command !-n Expand nth most recent command !n Expand nth command in history !<command> Expand most recent invocation of command <command>


!! and !$ can be replaced with any valid expansion.,aec7ee5e”>

) !^ Expand first argument from most recent command !$ Expand last token from most recent command !!:n-m Expand range of tokens from most recent command !!:n-$ Expand nth token to last from most recent command

!! can be replaced with any valid expansion i.,e. !cat, !-2, !42, ecc.,

Special variables

Expression Description
$? Exit status of last task
$! PID of last background task
$$ PID of shell
$0 Filename of the shell script

See Special parameters.,

pwd # /home/user/foocd bar/pwd # /home/user/foo/barcd -pwd # /home/user/foo

Check for command’s result

if ping -c 1; then echo "It appears you have a working internet connection"fi

Grep check

if grep -q 'foo' ~/.bash_history; then echo "You appear to have typed 'foo' in the past"fi

Also see

Author: admin

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *