Linked Questions
233 questions linked to/from How do I get the directory where a Bash script is located from within the script itself?
973
votes
23
answers
756k
views
Reliable way for a Bash script to get the full path to itself [duplicate]
I have a Bash script that needs to know its full path. I'm trying to find a broadly-compatible way of doing that without ending up with relative or funky-looking paths. I only need to support Bash, ...
200
votes
3
answers
236k
views
Relative paths based on file location instead of current working directory [duplicate]
Given:
some.txt
dir
|-cat.sh
With cat.sh having the content:
cat ../some.txt
Then running ./cat.sh inside dir works fine while running ./dir/cat.sh on the same level as dir does not. I expect this ...
151
votes
5
answers
198k
views
Determine the path of the executing Bash script [duplicate]
In a Windows command script, one can determine the directory path of the currently executing script using %~dp0. For example:
@echo Running from %~dp0
What would be the equivalent in a Bash script?
16
votes
2
answers
17k
views
Get parent directory of shell script's directory [duplicate]
I have a shell script here:
/node_modules/.bin/exec.sh
in the exec.sh script, I want to obtain the path of the directory's parent directory that the script is contained in (not pwd/cwd!). I can ...
16
votes
1
answer
11k
views
How to include bash scripts with relative path? [duplicate]
I have 3 scripts:
Script A:
echo "Hey you!"
Script B:
source ./A.sh
Script C:
source ./libs/B.sh
So scripts A and B in folder "libs" and script C use script B from this directory.
Script C throw ...
13
votes
2
answers
7k
views
Get current directory of file after getting called by another bash script [duplicate]
So I have one bash script which calls another bash script.
The second script is in a different folder.
script1.sh:
"some_other_folder/script2.sh"
# do something
script2.sh:
src=$(pwd) # THIS returns ...
5
votes
3
answers
2k
views
When Runtime.getRuntime().exec call linux batch file could not find its physical directory [duplicate]
I have an java application.And I use Runtime.getRuntime().exec for call a batch file.When I call a linux batch file using with Runtime.getRuntime().exec the batch file could not find its own directory....
4
votes
2
answers
5k
views
bash shell store path to current file as variable? [duplicate]
When starting a project in Python, I want to save some environment variables in a file called environment_variables and source this file in the bashrc.
The file looks something like this:
username=$...
3
votes
2
answers
2k
views
Create folder in same directory as shell script [duplicate]
I'm new to Shell Scripting:
I'm trying to create a script that asks for user input as to the folder name, finds the directory the script is being run from, and creates a folder within that same ...
0
votes
2
answers
3k
views
Bash script - trying to get path of script [duplicate]
I need to get the path of script.
I can do this manually with pwd but after searching online and I found this script:
DIR=$( cd $(dirname $0) ; pwd)
I don't know what it means. Can someone please ...
1
vote
1
answer
3k
views
Cron: Setting PATH from shell script not working [duplicate]
I wrote a simple shell script in centOS and executing this from a cron job.
My script looks like below:
#!/bin/bash
export PATH=$PATH:$(pwd)
echo $PATH
echo $PATH| mail -s "PATH" [email protected]
And ...
0
votes
3
answers
1k
views
How to run bash script from anywhere? [duplicate]
The problem is that I wrote a bash script that handle files. If I add this script to bashrc I can't find the file. I could write the absolute path but I would like to compress the directory and send ...
0
votes
2
answers
811
views
Assert shell script runs from desired directory [duplicate]
I have a small app I'm building and when I hand it off to other team members to work on I'd like to abstract out them having to execute the python script with args directly, and instead have them call ...
0
votes
2
answers
2k
views
How to find the location of the script that is being executed? [duplicate]
How can I find (if possible) the location of the script that is being executed?
So I have a script that I'll use to do some magic, and I'll need to call it from random locations on a system (this ...
0
votes
0
answers
1k
views
Bash - PWD not executing in current folder [duplicate]
I am using the following executable to download a zip from the web, extract it and then delete the zip.
#!/usr/bin/env bash
echo "Downloading project resources from Storage..."
filename="outfile....