SK Logo

Bash code snippets

Jul 5, 2020Last updated: May 3, 2021
Software Eng. in 🚀 tech

Articles

Snippets

Script Directory

To get the absolute path to directory in which the bash script resides

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

Multiline strings

Always forgetting this very important utility.

strvar=$(cat << EOF

# all content goes here, bash variables work.

EOF
)

To directly put the output into a file

cat << EOF > /path/to/file

# all content goes here, bash variables work.

EOF
)

© 2021 Sanyam Kapoor