0

Here is a part of my .bashrc file:

echo_pwd () {
    echo `pwd`
}
export echo_pwd="`echo_pwd`/"

I created a new function echo_pwd that prints the path of the current directory and a new environment variable that contains the current directory followed by a slash. Therefore, I can use my environment variable in command line (such as $PWD).

However, this environment variable doesn't expand correctly and always refers to my home directory:

~/Documents$ env | grep echo_pwd
echo_pwd=/home/pierre/
~/Documents$

What can I do to make my environment variable expands correctly?

Note: this is obviously a dummy example. In my specific use case, the echo_pwd is a much more complex function.

Pierre
  • 123
  • 1
  • 1
    I can't recreate what you're seeing. When I execute your function in a non-$HOME directory, the correct directory path is assigned to the variable echo_pwd. Are you not re-executing it when changing directories? Also, why don't you use $PWD? – Kusalananda Apr 01 '20 at 07:51
  • @Kusalananda As i said in my note at the end of the question, my function is much more complex and I simplify it for the question. Whatever, your comment helps me to understand where the issue is. The environment variable is set only when I open the terminal. If I open it in my home directory, even if I change the directory with cd, $echo_pwd will always be /home/pierre. Therefore, is there a way to have an environment variable (or something else) that can change dynamically? – Pierre Apr 01 '20 at 08:03
  • @KamilMaciorowski Indeed, if I use this syntax $(echo_pwd), I don't have to create an environment variable and its value is always updated when I change of directory. – Pierre Apr 01 '20 at 08:05
  • 1
    I think you expect the variable to update its value automatically based on the changing output of the function. That won't happen. – muru Apr 01 '20 at 08:45
  • I am wondering why an environment variable, and not just a variable? – ctrl-alt-delor Apr 01 '20 at 09:28
  • @ctrl-alt-delor If you know how to do it with a simple variable, I'm interested too. – Pierre Apr 01 '20 at 09:36

2 Answers2

1

In export echo_pwd="`echo_pwd`/", the command substitution is performed only once, when the variable is set.

If you need to update the value of the variable, you'd need to set it again.

In interactive bash, you could use PROMPT_COMMAND, which is executed each time the prompt is printed:

$ PROMPT_COMMAND='echo_pwd="`echo_pwd`/"'
$ cd bar/
$ echo $echo_pwd/
/tmp/foo/bar//
$ cd baz
$ echo $echo_pwd
/tmp/foo/bar/baz/
Kusalananda
  • 333,661
muru
  • 72,889
1

You're likely only executing the function and setting your variable's value once.

To automatically update the variable's value whenever you use cd, you may override the shell's built-in cd like so:

cd () {
    builtin cd "$@"
    echo_pwd=$( echo_pwd )/
}

This would call the built-in cd with whatever arguments you gave the cd function, and then update tha value of your variable (note that export is not needed if you have already exported it elsewhere, if it needs to be exported at all).

The complete setup could look like

echo_pwd () {
    printf '%s\n' "$PWD"    # or just: pwd
}
export echo_pwd="$( echo_pwd )/"

cd () {
    builtin cd "$@"
    echo_pwd=$( echo_pwd )/
}

I've opted for using printf with $PWD instead of calling the pwd utility, and I'm also using $(...) for command substitutions.

Kusalananda
  • 333,661