How to get the current path without last folder in a script?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:
/tmp/test/test2/test3/test.sh
path=$PWD
echo $path
Gives /tmp/test/test2/test3
.
But I want to exclude the folder where the sh script is in.
So my desired output would be:/tmp/test/test2
bash ubuntu
add a comment |Â
up vote
0
down vote
favorite
I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:
/tmp/test/test2/test3/test.sh
path=$PWD
echo $path
Gives /tmp/test/test2/test3
.
But I want to exclude the folder where the sh script is in.
So my desired output would be:/tmp/test/test2
bash ubuntu
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:
/tmp/test/test2/test3/test.sh
path=$PWD
echo $path
Gives /tmp/test/test2/test3
.
But I want to exclude the folder where the sh script is in.
So my desired output would be:/tmp/test/test2
bash ubuntu
I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:
/tmp/test/test2/test3/test.sh
path=$PWD
echo $path
Gives /tmp/test/test2/test3
.
But I want to exclude the folder where the sh script is in.
So my desired output would be:/tmp/test/test2
bash ubuntu
bash ubuntu
edited Aug 15 at 9:00
asked Aug 15 at 8:50
membersound
1066
1066
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
try the following code,
dirname "$(pwd)"
- dirname - strip suffix from file/dir path
leave the variable empty. I'm onubuntu
if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
â membersound
Aug 15 at 9:00
add a comment |Â
up vote
1
down vote
You may use
dir=$PWD%/* # or: dir=$( dirname "$PWD" )
or
dir="$PWD/.."
or
dir=".."
In the first case, the PWD
variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last /
and everything after it.
In the second case, the PWD
variable is used to simply create a path for the directory above using ..
.
In the third case, we simply refer to the directory above using a relative path from the current directory.
Which alternative you use is dependent on what you are going to use the path for.
Examples:
$ echo $PWD
/tmp/shell-sh.jAtMYgn0
$ dir=$PWD%/*
$ echo "$dir"
/tmp
$ dir="$PWD/.."
$ echo "$dir"
/tmp/shell-sh.jAtMYgn0/..
$ dir=".."
$ echo "$dir"
..
add a comment |Â
up vote
-1
down vote
Finally it was as simple as: current=$(dirname "$(pwd)")
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
try the following code,
dirname "$(pwd)"
- dirname - strip suffix from file/dir path
leave the variable empty. I'm onubuntu
if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
â membersound
Aug 15 at 9:00
add a comment |Â
up vote
2
down vote
try the following code,
dirname "$(pwd)"
- dirname - strip suffix from file/dir path
leave the variable empty. I'm onubuntu
if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
â membersound
Aug 15 at 9:00
add a comment |Â
up vote
2
down vote
up vote
2
down vote
try the following code,
dirname "$(pwd)"
- dirname - strip suffix from file/dir path
try the following code,
dirname "$(pwd)"
- dirname - strip suffix from file/dir path
edited Aug 15 at 9:01
answered Aug 15 at 8:56
msp9011
3,46643862
3,46643862
leave the variable empty. I'm onubuntu
if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
â membersound
Aug 15 at 9:00
add a comment |Â
leave the variable empty. I'm onubuntu
if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
â membersound
Aug 15 at 9:00
leave the variable empty. I'm on
ubuntu
if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.â membersound
Aug 15 at 9:00
leave the variable empty. I'm on
ubuntu
if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.â membersound
Aug 15 at 9:00
add a comment |Â
up vote
1
down vote
You may use
dir=$PWD%/* # or: dir=$( dirname "$PWD" )
or
dir="$PWD/.."
or
dir=".."
In the first case, the PWD
variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last /
and everything after it.
In the second case, the PWD
variable is used to simply create a path for the directory above using ..
.
In the third case, we simply refer to the directory above using a relative path from the current directory.
Which alternative you use is dependent on what you are going to use the path for.
Examples:
$ echo $PWD
/tmp/shell-sh.jAtMYgn0
$ dir=$PWD%/*
$ echo "$dir"
/tmp
$ dir="$PWD/.."
$ echo "$dir"
/tmp/shell-sh.jAtMYgn0/..
$ dir=".."
$ echo "$dir"
..
add a comment |Â
up vote
1
down vote
You may use
dir=$PWD%/* # or: dir=$( dirname "$PWD" )
or
dir="$PWD/.."
or
dir=".."
In the first case, the PWD
variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last /
and everything after it.
In the second case, the PWD
variable is used to simply create a path for the directory above using ..
.
In the third case, we simply refer to the directory above using a relative path from the current directory.
Which alternative you use is dependent on what you are going to use the path for.
Examples:
$ echo $PWD
/tmp/shell-sh.jAtMYgn0
$ dir=$PWD%/*
$ echo "$dir"
/tmp
$ dir="$PWD/.."
$ echo "$dir"
/tmp/shell-sh.jAtMYgn0/..
$ dir=".."
$ echo "$dir"
..
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You may use
dir=$PWD%/* # or: dir=$( dirname "$PWD" )
or
dir="$PWD/.."
or
dir=".."
In the first case, the PWD
variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last /
and everything after it.
In the second case, the PWD
variable is used to simply create a path for the directory above using ..
.
In the third case, we simply refer to the directory above using a relative path from the current directory.
Which alternative you use is dependent on what you are going to use the path for.
Examples:
$ echo $PWD
/tmp/shell-sh.jAtMYgn0
$ dir=$PWD%/*
$ echo "$dir"
/tmp
$ dir="$PWD/.."
$ echo "$dir"
/tmp/shell-sh.jAtMYgn0/..
$ dir=".."
$ echo "$dir"
..
You may use
dir=$PWD%/* # or: dir=$( dirname "$PWD" )
or
dir="$PWD/.."
or
dir=".."
In the first case, the PWD
variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last /
and everything after it.
In the second case, the PWD
variable is used to simply create a path for the directory above using ..
.
In the third case, we simply refer to the directory above using a relative path from the current directory.
Which alternative you use is dependent on what you are going to use the path for.
Examples:
$ echo $PWD
/tmp/shell-sh.jAtMYgn0
$ dir=$PWD%/*
$ echo "$dir"
/tmp
$ dir="$PWD/.."
$ echo "$dir"
/tmp/shell-sh.jAtMYgn0/..
$ dir=".."
$ echo "$dir"
..
answered Aug 15 at 9:04
Kusalananda
106k14209327
106k14209327
add a comment |Â
add a comment |Â
up vote
-1
down vote
Finally it was as simple as: current=$(dirname "$(pwd)")
add a comment |Â
up vote
-1
down vote
Finally it was as simple as: current=$(dirname "$(pwd)")
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Finally it was as simple as: current=$(dirname "$(pwd)")
Finally it was as simple as: current=$(dirname "$(pwd)")
answered Aug 15 at 9:11
membersound
1066
1066
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462701%2fhow-to-get-the-current-path-without-last-folder-in-a-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password