shell-init: error retrieving current directory: getcwd -- The usual fixes do not wor shell-init: error retrieving current directory: getcwd -- The usual fixes do not wor bash bash

shell-init: error retrieving current directory: getcwd -- The usual fixes do not wor


I believe the error is not related to the script at all. The issue is: the directory at which you are when you try to run the script does not exist anymore. for example you have two terminals, cd somedir/ at the first one then mv somedir/ somewhere_else/ at the second one, then try to run whatsoever in the first terminal - you'll receive this error message.

Please note you'll get this error even if you re-create directory with the same name because the new directory will have different inode index.

At least this was in my case.


According to this answer, it happens when "you re-create directory" but already have a shell at this location, so we could simply run:

cd ~ && cd -


You are executing this as a script.. $(~/.ansible/ansible_hosts). The $() means that bash will attempt to execute that script and then output the results.

But it's not a script, right? It's a list of hosts!

Just add the word cat and it should work.

#!/bin/bashfor server in $(cat ~/.ansible/ansible_hosts)do    ssh $server "hostname; readlink /opt/mydir/mylink;"done