Skip to content

Instantly share code, notes, and snippets.

@cirias
Last active August 29, 2015 14:18
Show Gist options
  • Save cirias/c66eadd640fbf54f1cbf to your computer and use it in GitHub Desktop.
Save cirias/c66eadd640fbf54f1cbf to your computer and use it in GitHub Desktop.
bash-script-tips.md

Header
#!/usr/bin/env bash

Condition
if [[ condition ]]; then
  statement
fi

List
listVar="1 2 3"
for i in $listVar; do
  echo $i
done

Read
read VAR
echo $VAR

Test file exists
-e $filename

Test directory exists
-d $dir

Fail cause exit immediately
set -e

Bash Switch
case "$C" in
"1")
    do_this()
    ;;
"2" | "3")
    do_what_you_are_supposed_to_do()
    ;;
*)
    do_nothing()
    ;;
esac

Can a Bash script tell what directory it's stored in?

http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

Turn off the substitution

http://serverfault.com/questions/208265/what-is-bash-event-not-found

set +H

If success then else
cmd && echo success || echo fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment