Shell Script believes that the directory does not exist when starting cd

I have a shell script (which I source in .bashrc) that allows me to go to the project directory from anywhere.

cdp(){
  proj="~/dev/projects/$@/"
  builtin cd $proj
}

_my_cdp()
{
  local cur opts
      cur="${COMP_WORDS[COMP_CWORD]}"
      opts=$(ls ~/dev/projects/)
      COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
}

complete -o nospace -F _my_cdp cdp

The problem is that cdon line 3 it says:

bash: cd: ~/dev/projects/jsonparse/: No such file or directory

Here is the full console output showing the error, and proof of the existence of the directory.

amflare:~$ cd dev/projects/ (go to dir)
amflare:~/dev/projects$ ls -al (look at contents)
total 68
drwxrwxr-x 17 www-data amflare 4096 Dec 29 13:11 .
drwxrwxr-x  4 amflare amflare 4096 Dec  6 17:32 ..
drwxrwxr-x  3 www-data amflare 4096 Dec 22 17:33 bot
drwxrwxr-x  3 www-data amflare 4096 Dec 20 15:17 jsonparse
drwxrwxr-x  3 www-data amflare 4096 Dec 28 19:58 magic
drwxrwxr-x  2 www-data amflare 4096 Nov 11 14:42 test
amflare:~/dev/projects$ cd (go to home)
amflare:~$ cdp (run autocomplete)
bot         jsonparse   magic       test
amflare:~$ cdp jsonparse (pick target)
bash: cd: ~/dev/projects/jsonparse/: No such file or directory
amflare:~$ (still in home)

I tried everything I could come up with and a few things google gave for other distributions (I'm on Ubuntu Gnome 16.04). No matter what I do, the shell script does not recognize the existence of anything inside ~/dev/projects/. Also, it does not work when I have functions in .bashrc, so I don't think this is a subshell problem. Please let me know if you need more information. Thank.

+4
1

, . proj=~/"dev/projects/$@/" . HOME: proj="$HOME/dev/projects/$@". , $@ . , ?


, opts=$(ls ~/dev/projects/). compgen :

cdp() {
  local proj=~/dev/projects/
  builtin cd "$proj$1"
}

_my_cdp() {
    local proj=~/dev/projects/
    local i p
    COMPREPLY=()
    while IFS= read -r i; do
        printf -v p '%q' "${i#"$proj"}"
        COMPREPLY+=( "$p" )
    done < <(compgen -d -- "$proj$2")
}

_my_cdp $2 , ; compgen -d ( $proj) ( - , , ). printf '%q', $proj. , , (, glob), COMPREPLY . ( 100% , !).

+5

Source: https://habr.com/ru/post/1665358/


All Articles