Jeegar Patel
Jeegar Patel

Reputation: 27210

readlink command misbehave after issueing pushd command in bash scripts

I came across one complex problem I tried to explain it with simple example as below

In my system I have

ubuntu@ubuntu:~/temp$ pwd
/home/ubuntu/temp
ubuntu@ubuntu:~/temp$ ls
temp1  test.sh
ubuntu@ubuntu:~/temp$ 

In temp.sh I have

#!/bin/bash

echo "Arg 0 = $0"
echo "Arg 1 = $1"
echo "Arg 0 Full Path $(readlink -f $0)"
echo "Arg 1 Full Path $(readlink -f $1)"
pushd /var/log
echo "Arg 0 = $0"
echo "Arg 1 = $1"
echo "Arg 0 Full Path $(readlink -f $0)"
echo "Arg 1 Full Path $(readlink -f $1)"

now I run at below way

ubuntu@ubuntu:~/temp$ ./test.sh temp1
Arg 0 = ./test.sh
Arg 1 = temp1
Arg 0 Full Path /home/ubuntu/temp/test.sh
Arg 1 Full Path /home/ubuntu/temp/temp1
/var/log ~/temp
Arg 0 = ./test.sh
Arg 1 = temp1
Arg 0 Full Path /var/log/test.sh
Arg 1 Full Path /var/log/temp1

Here you can see readlink shows wrong path of the Arg0 and Arg1 files after issuing pushd command. If I remove popd command then it prints fine.

So why here readlink misbehave?

Upvotes: 0

Views: 490

Answers (2)

rslemos
rslemos

Reputation: 2731

Given relative paths, readlink will interpret them relative to process working directory, and output absolute paths (resolving symbolic links inbetween).

The key point here is the process working directory (aka current directory).

So readlink ./path/to/file will output /tmp/path/to/file if /tmp is the current directory (supposing no symbolic links).

The other command you're using, pushd will change the process working directory.

So in the sequence

readlink ./path/to/file
pushd /some/other/place
readlink ./path/to/file

both readlink will likely resolve to two different absolute paths.

No misbehavior here. All by design.

Upvotes: 1

sumitya
sumitya

Reputation: 2691

readlink is behaving correctly here, only thing needs to be understood here is the behaviour of pushd. pushd is the command to change current directory stack. Let's understand from below picture.

enter image description here

Initially test.sh had some full path, after running pushd one more directory i.e (/var/log) is inserted into directory stack . Left most directory( or top most directory ) of stack becomes current directory. And if you run popd , that means stack is getting empty from the top. As soon as you run readlink -f test.sh again after popd , you will have initial directory . In your case it will be /home/ubuntu/temp/test.sh

Upvotes: 1

Related Questions