-1

So I'm not very educated on Bash but I've been stumped on this. The switchto function is to provide me an easy way to jump between projects on the command line.

source ~/.profile
export PATH=~/bin:$PATH
export PATH=~/.composer/vendor/bin:$PATH
alias composer="php /usr/local/bin/composer.phar"

switchto () (
        cd "~/Projects/$1.com/wp-content/themes/$1"
)

Everything SEEMS right to me, but it keeps saying if I run for example switchto example:

-bash: cd: ~/Projects/example.com/wp-content/themes/example: No such file or directory

I've verified again and again the the directory does indeed exist. I'm new to bash so I'm not sure if it's something simple I'm doing wrong?

1 Answers1

3

Your tilde ~ is not expanded to your home directory if it is quoted.

https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html

Tilde Expansion

If a word begins with an unquoted tilde character (~), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the HOME shell variable. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.

So you can use (with curly braces):

switchto () {
    cd ~/"Projects/$1.com/wp-content/themes/$1"
}

and you can combine your two PATH definitions with

  export PATH=~/bin:~/.composer/vendor/bin:$PATH
Freddy
  • 25,565