Skip to content

Instantly share code, notes, and snippets.

@alifarazz
Last active October 7, 2025 21:37
Show Gist options
  • Select an option

  • Save alifarazz/83706a888806589812c6ef97946ad4bf to your computer and use it in GitHub Desktop.

Select an option

Save alifarazz/83706a888806589812c6ef97946ad4bf to your computer and use it in GitHub Desktop.
Get path of the currently running script in BASH. Also supports SLURM
#!/usr/bin/env bash
# Get path to current script,
if [ -n "${SLURM_JOB_ID:-}" ] ; then
SCRIPT_PATH="$(scontrol show job "$SLURM_JOB_ID" | awk -F= '/Command=/{print $2}')"
else
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
fi
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"
echo "$SCRIPT_PATH"
echo "$SCRIPT_DIR"
# source: https://stackoverflow.com/a/56991068
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment