It's similar to Windows Batch, in that it's built to string command-line expressions together.
Batch is based on MS-DOS
Bash is built on the POSIX standard
The downside of Bash is that if any commands update and change their output, scripts will easily break
!# is called a
shebang
#!/usr/bin/env bashEven though bash is often just in /usr/bin/, coding in /usr/bin/etc tells the kernel to seach PATH,
incase bash is somewhere else.
By default, if a line in a bash script fails, Bash will ignore it and keep going.
set -e: Tells bash to end if it
encounters any errors (non-zero exit codes).
MODE="${1:-default}"$1 represents the first positional
argument passed to the script
$1:-default tells the program to
evaluate variable one, and if it doesn't exist, to use the string
'default' for its default value
[ is a command alias for test
[ -d "$REPO" ] checks that the repo is
a valid directory
[ is itself a command$(command) is command
substitution
git push origin main >> "LOG_FILE"Bash first reroutes the output stream to LOG_FILE and then executes the command
>> opens the file in append
mode> opens it in write modegit then writes its output to stdout as usual, but Bash has already pointed stdout to the log file.
Every process of Linux gets automatically assigned 3 communication channels (file descriptors)
2>&1 means redirect file
descriptor 2's stream to the current target of file descriptor
1
Bash reads local variables dynamically by default (unlike Batch)
Unlike Batch, Bash actually has arrays natively
${REPOS[@]} tells Bash to unpack the
entire array
Semicolons are syntactically equivalent to new lines.
if [ "$MODE" == "pull" ]; thenif ! git diff-index --quiet HEAD --; then! is a logical inversion acting on
git diff-index