Only the most common stuff
- Index:
- Ref:
# 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}" )You can't. You have two options:
- Treat it like a vararg, passing the expanded version:
function foo () { local bar="$1" shift local baz="$1" shift local rest=("$@") } neet=(bing bang boom) foo "bar" "baz" "${neet[@]}"
- Pass it as a
nameref:function foo () { local bar="$1" local baz="$2" local -n rest="$3" } neet=(bing bang boom) foo "bar" "baz" "neet"
FOO=$(echo "$FOO" | xargs)