Only the most common stuff
# Doesn't work if "this file" is a symlink
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Doesn't work if you're trying to "source" this script
SCRIPT_DIR=$(dirname -- "$( readlink -f -- "$0"; )";)
# Works everywhere, but since this does `cd` out you may not be able to `cd` back in
pushd . > '/dev/null';
SCRIPT_PATH="${BASH_SOURCE[0]:-$0}";
while [ -h "$SCRIPT_PATH" ];
do
cd "$( dirname -- "$SCRIPT_PATH"; )";
SCRIPT_PATH="$( readlink -f -- "$SCRIPT_PATH"; )";
done
cd "$( dirname -- "$SCRIPT_PATH"; )" > '/dev/null';
SCRIPT_PATH="$( pwd; )";
popd > '/dev/null';
: ${REQVAR:?[You must set REQVAR]}
: ${MAYBEVAR:='default value'}
#!/bin/bash
otherprog "$@" # equvalent to "$1" "$2" ...
otherprog $* # same thing
otherprog "$*" # equivalent to "$1 $2"
All but the first:
#!/bin/bash
shift
otherprog "$@" # equvalent to "$2" "$3" ...
my_arr=(foo bar baz)
my_arr+=(bing boom) #append
arr_size=${#my_arr[@]} #count
# By value
for v in ${my_arr[@]}; do
echo $v
done
# By index
for i in ${!my_arr[@]}; do
echo ${my_arr[$i]}
done
# delete the 'foo' element from my_arr
my_arr=( "${my_arr[@]/foo}" )
or
delete='foo'
my_arr=( "${my_arr[@]/$delete}" )
FOO=$(echo "$FOO" | xargs)