Bash modules

From wikinotes
Revision as of 17:40, 6 March 2020 by Will (talk | contribs) (→‎Commandline Arguments)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

shebang

Windows uses file-extensions to determine what program to use to open/run a program. Unix systems use mimetypes, which uses a combination of file-extensions, and signatures written within the file. This means that file-extensions are optional for bash-scripts.

Place this line at the top of your file to instruct your system that this file should be opened with an executable called bash. You can substitute this with something else, for example this might be a python, perl, or ruby script.

#!/usr/bin/env bash

exit on first error

place near top of file.

set -e

Finding Path of Running Bash Script

currentPath="`( cd \"$currentPath\" && pwd )`"

#OR

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
## This works as long as you are not using a symbolic link. In the event that you
## are using a symbolic link:

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

Commandline Arguments

See bash arguments .