Linked Questions

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, ...
T.J. Crowder's user avatar
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 ...
ben's user avatar
  • 6,113
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?
Atif Aziz's user avatar
  • 36.7k
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 ...
Alexander Mills's user avatar
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 ...
Kirill Rud's user avatar
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 ...
Travv92's user avatar
  • 801
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....
fatih emrah durum's user avatar
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=$...
William Grimes's user avatar
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 ...
user avatar
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 ...
Closed Eagle's user avatar
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 ...
Mahbub Rahman's user avatar
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 ...
SzAbel's user avatar
  • 33
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 ...
Ethan Fox's user avatar
  • 169
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 ...
Sahil Soni's user avatar
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....
John Doe's user avatar
  • 460

15 30 50 per page
1
2 3 4 5
16