'zsh using a variable in a command within a function
In .zsh, in my .zshrc file I'd like to set up a function to cd to a directory I input, but using an existing variable to write the common ~/path/to/parent/directory/$input
I've been unable to find out what the correct syntax is for this particular usage. For example, I want to enter
goto mydir
and execute a cd
to ~/path/to/parent/directory/mydir
But I get an error: gt:cd:3 no such file or directory ~/path/to/parent/directory/mydir even though that directory exists.
This is the variable declaration and function I am trying:
export SITESPATH="~/path/to/parent/directory"
function gt(){
echo "your site name is $@"
echo "SITESPATH: " $SITESPATH "\n"
cd $SITESPATH/$@
}
It makes no difference if I use the above, without quotes, or "cd $SITESPATH/$@"
with quotes.
Solution 1:[1]
I don't see the point in using $@
in your function, since you expect only one argument. $1
would be sufficient.
The problem is in the tilde which is contained in your variable SITEPATH
. You need to have it expanded. You can either do it by writing
export SITESPATH=~/path/to/parent/directory
when you define the variable, or inside your function by doing a
cd ${~SITESPATH)/$1
A third possibility is to turn on glob_subst
in your shell:
setopt glob_subst
In this case, you can keep your current definition of $SITESPATH
, and tilde-substitution will happen automatically.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user1934428 |