Linux echo current path

Linux pwd Command

The pwd is a helpful command for Linux users who are inexperienced and might get lost in the middle of directories.

The pwd is an abbreviated form of “Print Working Directory,” or we can call the Current Work Directory. It displays the name of the current directory with the root path.

It is the most frequently used built-in shell command by Linux system administrators.

PWD Syntax

The Syntax of pwd Command is:

PWD Commands

Here are a few most commonly used commands of pwd. Let’s check how they work in Linux System:

Display Current Directory

As my current working directory is the home directory, so it will print the home directory in the terminal.

To display the current working directory in Linux, use the command:

PWD Flags

The pwd command accepts two flags:

1. pwd –L
The -L flag is used to print symbolic links; in other words, in Linux, it points towards the file or folder where you are currently working in.

Let’s check its functionality:

Create a symbolic link in a home directory named Linuxhint and move the present directory to the newly created one.

Now, use the given command to check if it’s working:

2. pwd –P:
The -P command is used to print the actual path without any symbolic link.

pwd Version

The pwd command comes pre-installed. You can check the pwd version through the command-line by given command:

pwd -a

The following command will help you to display the list of all locations having executable name pwd:

pwd Help

When you use the help command, it will show you pwd command options.

To get it, use mentioned command in the terminal:

Echo $PWD

To store the path of the current directory, the echo $PWD command is used. It functions the same as the pwd –L command:

PWD and OLDPWD command

To get current and previous directory using one command, type:

Conclusion

We have seen a brief explanation of the pwd command and its examples. The pwd command is a widely used shell built-in command in Linux systems. It displays the current working directory on which the user is working.

Читайте также:  Linux клавиатура как мышь

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

How can I get the current working directory? [duplicate]

I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd . It just prints pwd how do I get the current working directory into a variable?

This is not a duplicate of the question for which it is currently marked as one. The two questions should be compared, at least, based on their titles (as well as their answers). That the answer to this question is already covered by another is, or should be, irrelevant.

@KennyEvitt actually, one of the main reasons we close is precisely because an answer has been given elsewhere. And, in fact, the main question here is actually how to assign the output of a command to a variable, which is covered by the dupe. I have also given the answer to this specific case, so all bases are covered. There would be no benefit in opening this again.

@terdon As a resource available, and intended, for the entire population of Unix & Linux users, this is a valuable question, even if the original asker really just needed an answer already covered elsewhere. If anything, I think this question should be edited to more closely match its title and it should be re-opened, not to allow further activity, but to not imply that this question is ‘bad’.

@KennyEvitt closing as a duplicate in no way implies that the question is bad! This question will remain here, answered, for ever. If you really want to know how to get the current working directory, you will find your answer here. If you just want to know how to save the output of a command in a variable, you will also find the answer here by following the link to the dupe. In any case, this isn’t really something I should do alone, if you feel strongly that it should be reopened, please open a discussion on Unix & Linux Meta where such things should be resolved.

5 Answers 5

There’s no need to do that, it’s already in a variable:

The PWD variable is defined by POSIX and will work on all POSIX-compliant shells:

Set by the shell and by the cd utility. In the shell the value shall be initialized from the environment as follows. If a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory that is no longer than bytes including the terminating null byte, and the value does not contain any components that are dot or dot-dot, then the shell shall set PWD to the value from the environment. Otherwise, if a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory, and the value does not contain any components that are dot or dot-dot, then it is unspecified whether the shell sets PWD to the value from the environment or sets PWD to the pathname that would be output by pwd -P. Otherwise, the sh utility sets PWD to the pathname that would be output by pwd -P. In cases where PWD is set to the value from the environment, the value can contain components that refer to files of type symbolic link. In cases where PWD is set to the pathname that would be output by pwd -P, if there is insufficient permission on the current working directory, or on any parent of that directory, to determine what that pathname would be, the value of PWD is unspecified. Assignments to this variable may be ignored. If an application sets or unsets the value of PWD, the behaviors of the cd and pwd utilities are unspecified.

For the more general answer, the way to save the output of a command in a variable is to enclose the command in $() or ` ` (backticks):

Читайте также:  Linux mint жесткие ссылки

Of the two, the $() is preferred since it is easier to build complex commands like:

command0 "$(command1 "$(command2 "$(command3)")")" 

Whose backtick equivalent would look like:

command0 "`command1 \"\`command2 \\\"\\\`command3\\\`\\\"\`\"`" 

Источник

How to Get Current Directory in Shell Script

get current directory in linux

Sometimes you may need to get current working directory in shell script. You can easily do this using pwd command or PWD environment variable. In this article, we will look at how to get current directory in shell script using these two methods.

How to Get Current Directory in Shell Script

You can use built-in shell command pwd or shell variable $PWD to get current working directory as per your requirement. We will look at how to use both of them below.

Here is an example of how to use them in shell terminal.

$ pwd /home/ubuntu $ echo $PWD /home/ubuntu

Now we will create a shell script to demonstrate the above two ways to get current directory.

1. Create Shell Script

Open terminal and run the following command to create a blank shell script.

2. Get Current Directory

Add the following lines to your Shell Script.

!/bin/sh echo "current directory using pwd command" echo $(pwd) echo "current directory using PWD variable" echo $PWD echo "previous working directory using OLDPWD variable" echo $OLDPWD

Save and close the file. In the above code, we specify shell script execution environment. Then we display current path using pwd command. Next, we display present working directory using PWD shell variable. Finally, we display the previous working directory using OLDPWD shell variable.

Читайте также:  Kali linux артефакты при установке

3. Make Shell Script Executabe

Run the following command to make it executable.

$ sudo chmod +x current_dir.sh

4. Run shell script

Run the shell script to verify output

$ ./current_dir.sh current directory using pwd command /home/ubuntu current directory using PWD variable /home/ubuntu previous working directory using OLDPWD variable /etc/data

You may also store the result of either of these commands in a variable and append strings to get more path values. Here is an example

#!/bin/sh path =$PWD echo $PWD #/home/ubuntu subpath="/data" new_path = $path$subpath #/home/ubuntu/data echo $new_path #/home/ubuntu/data

In this article, we have learnt how to get current working directory in shell script using pwd command as well as using PWD environment variable. You may use either of them depending on your requirement. They both give same result. You may also append other strings to them to construct new path values.

Источник

Get current directory or folder name (without the full path)

How could I retrieve the current working directory/folder name in a bash script, or even better, just a terminal command. pwd gives the full path of the current working directory, e.g. /opt/local/bin but I only want bin .

24 Answers 24

No need for basename, and especially no need for a subshell running pwd (which adds an extra, and expensive, fork operation); the shell can do this internally using parameter expansion:

result=$ # to assign to a variable result=$ # to correct for the case where PWD=/ printf '%s\n' "$" # to print to stdout # . more robust than echo for unusual names # (consider a directory named -e or -n) printf '%q\n' "$" # to print to stdout, quoted for use as shell input # . useful to make hidden characters readable. 

Note that if you’re applying this technique in other circumstances (not PWD , but some other variable holding a directory name), you might need to trim any trailing slashes. The below uses bash’s extglob support to work even with multiple trailing slashes:

dirname=/path/to/somewhere// shopt -s extglob # enable +(. ) glob syntax result=$ # trim however many trailing slashes exist result=$ # remove everything before the last / that still remains result=$ # correct for dirname=/ case printf '%s\n' "$result" 

Alternatively, without extglob :

dirname="/path/to/somewhere//" result="$">" # extglob-free multi-trailing-/ trim result="$" # remove everything before the last / result=$ # correct for dirname=/ case 

Источник

Оцените статью
Adblock
detector