If you're using bash (i.e. the script starts with #!/bin/bash
), you can use $BASH_SOURCE to get the filename of the script. From that, you can get the directory it's in:
mydir="$(dirname "$BASH_SOURCE")"
...and then use that to find files relative to the script, e.g. cp "$mydir/fileInTheSameFolder" /tmp
(and please always use double-quotes around it, as I did here).
Note that this may be a relative path; for example, if the script was run from an interactive shell with ./scriptname.command
, it'll just come out as ".". This shouldn't be a problem unless the script cd's somewhere else, but if you need the full path you can use this instead:
mydir="$(cd "$(dirname "$BASH_SOURCE")" && pwd)" || {
echo "Error getting script directory" >&2
exit 1
}
Or, could just cd
to the script's directory at the beginning of the script:
cd "$(dirname "$BASH_SOURCE")" || {
echo "Error getting script directory" >&2
exit 1
}