Bash scripting

Keyboard shortcuts

Command Line Editing
Commands For Moving
Mastering The Linux Shell – Bash Shortcuts Explained (Now With Cheat Sheets)
Bash Keyboard Shortcuts

[CTRL + P]      Print previous command (Up arrow)
[CTRL + N]      Print next command (Down arrow)
[CTRL + R]      History search
[ALT  + R]      Cancel changes, put back like in the history
[!    + !]      Repeat last command
[!    + <cmd>]  Run last command starting with <cmd>
[!    + *]      Run all argument of previous command
[!    + $]      Run last argument of previous command
[ALT  + .]      Print last argument of previous command
[^<p1> + ^<p2>] Run last command, replace <p1> with <p2>
[CTRL + F]      Move forward one character (Right arrow)
[CTRL + B]      Move backward one character (Left arrow)

[ALT  + F]      Move forward one word
[ALT  + B]      Move backward one word

[CTRL + A]      Move to the start of the line
[CTRL + E]      Move to the end of the line

[CTRL + X + X]  Toggle between start of word and cursor
[CTRL + H]      Delete previous character from the cursor
                (= Rubout, like Backspace)
[CTRL + D]      Delete current character from the cursor (Delete)
[CTRL + K]      Cut from the cursor to the end of the line
[CTRL + U]      Cut from the cursor to the start of the line
                (like [CTRL + X + BACKSPACE]

[ALT  + D]      Cut from the cursor to the end of the current word
[CTRL + W]      Cut from the cursor to the previous whitespace

[CTRL + Y]      Paste
[ALT  + T]      Swap current word with previous (or [ESC + T])
[CTRL + T]      Swap the last two characters before the cursor (typo)

[ALT  + U]      Upper-case word and move to the end of the word
[ALT  + L]      Lower-case word and move to the end of the word
[ALT  + C]      Capitalize word and move to the end of the word

Job ID / Percent sign (%)

Percent sign (%) in front of shell command

$ tail -f /var/log/messages
[CTRL + Z]
[1]+  Stopped                 tail -f /var/log/messages

$ tail -f /var/log/maillog
[CTRL + Z]
[2]+  Stopped                 tail -f /var/log/maillog

$ jobs
[1]-  Stopped                 tail -f /var/log/messages
[2]+  Stopped                 tail -f /var/log/maillog

$ fg %2
tail -f /var/log/maillog
[CTRL + Z]
[2]+  Stopped                 tail -f /var/log/maillog

$ fg %1
tail -f /var/log/messages
[CTRL + Z]
[1]+  Stopped                 tail -f /var/log/messages

$ kill %1 %2
[1]-  Terminated              tail -f /var/log/messages
[2]+  Terminated              tail -f /var/log/maillog

Process List (ps)

$ ps auxf
$ ps auxd

#!/usr/bin/env

Howto Make Script More Portable With #!/usr/bin/env As a Shebang

#!/usr/bin/env bash

rather than

#!/bin/bash

Bash Conditional Expressions
The Set Builtin

Default values

APP_NAME=${APP_NAME:-"default_name"}

$ export FOO=first
$ echo "The ${FOO:-second} choice"
The first choice

$ unset FOO
$ echo "The ${FOO:-second} choice"
The second choice

$ export FOO=
$ echo "The ${FOO:-second} choice"
The second choice

Default values
bash assign default value

Parameter Expansion

Parameter expansion
Parameter Expansion

  • Simple usage
  • Indirection
  • Case modification
  • Variable name expansion
  • Substring removal
  • Search and replace
  • String length
  • Substring expansion
  • Use a default value
  • Assign a default value
  • Use an alternate value
  • Display error if null or unset

Bash : removing part of a string (= Parameter Expansion)

From the beginning:
path1="/usr/local/bin/bash"
${PARAMETER#PATTERN}  => shortest matching: ${path1#/*}  => usr/local/bin/bash
${PARAMETER##PATTERN} => longest matching:  ${path1##*/} => bash

From the end:
path2="x/usr/local/bin/bash"
${PARAMETER%PATTERN}  => shortest matching: ${path2%/*}  => x/usr/local/bin
${PARAMETER%%PATTERN} => longest matching:  ${path2%%/*} => x

var="Memory Used: 19.54M"
var=${var#*: }            # Remove everything up to a colon and space
var=${var%M}              # Remove the M at the end

Lists of Commands, Command Sequences

Lists of Commands
Writing Better Shell Scripts – Part 2
Meaning of colon in Bash after a double pipe
Bourne Shell Builtins
Is there a difference between how two ampersands and a semi-colon operate in bash?

  • Two logical short-circuits are the double ampersand (&&) and double pipe (||) operators.
  • The && only allows the command that comes after it in the series to be executed if the previous command exited with a status of 0.
  • The || operator does the opposite by only allowing the next command to be executed if the previous one returned a non-zero exit status.
  • The ; just separates one command from another.
  • The : is a null statement, so it does nothing.
  • The . executes a script in the current shell, not starting a new shell
$ true; echo $?
0

$ false; echo $?
1


=== && ===
$ true && echo "hello"
hello

$ false && echo "hello"
<no output>


=== || ===
$ true || echo "hello"
<no output>

$ false || echo "hello"
hello


=== ; ===
$ true; echo "hallo"
hallo

$ false; echo "hallo"
hallo


=======================

[ -n STRING ] => True if the length of "STRING" is non-zero.

=== return value ===
$ [ -n "hallo" ]; echo $?
0

$ [ -n "" ]; echo $?
1


=== if then ===
$ if [ -n "hallo" ]; then echo "welt"; fi
welt

$ if [ -n "" ]; then echo "welt"; fi
<no output>


=== && ===
$ [ -n "hallo" ] && echo "welt"
welt

$ [ -n "" ] && echo "welt"
<no output>


=== || ===
$ [ -n "hallo" ] || echo "welt"
<no output>

$ [ -n "" ] || echo "welt"
welt

Bang dollar-sign

Advancing in the Bash Shell

$ ./app a b c d e f

$ !*
a b c d e f
-bash: a: command not found

$ !$
f
-bash: f: command not found

Brace Expansion

Advancing in the Bash Shell

$ cp filename filename-old
$ cp filename-old filename
$ cp filename{,-old}
$ cp filename{-old,}
$ cp filename{-v1,-v2}

Exit code

Exit and Exit Status
Exit Codes With Special Meanings
Understanding Exit Codes and how to use them in bash scripts

$ bla
-bash: bla: command not found

$ echo $?
127
Exit Code Number Meaning Example Comments
1 Catchall for general errors let “var1 = 1/0” Miscellaneous errors, such as “divide by
zero”
and other impermissible operations
2 Misuse of shell builtins (according to Bash documentation) empty_function() {} Missing keyword
or command, or permission problem (and diff return code
on a failed binary file comparison
).
126 Command invoked cannot execute /dev/null Permission problem or command is not an executable
127 “command not found” illegal_command Possible problem with $PATH or a typo
128 Invalid argument to exit exit 3.14159 exit takes only integer args in the
range 0 – 255 (see
first footnote)
128+n Fatal error signal “n” kill -9 $PPID of script $? returns
137 (128 + 9)
130 Script terminated by Control-C Ctl-C Control-C is fatal error signal
2, (130 = 128 + 2, see above)
255* Exit status out of range exit -1 exit takes only integer args in the
range 0 – 255

Leave a Reply

Your email address will not be published. Required fields are marked *