Using dir var in an alias?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a directory variable and function as follows:
coding_dir="~/Documents/coding"
function f() cd $1 && ls -a ;
And I want to create an alias as follows:
alias rnd="f $coding_dir/random"
But when I try using the alias, I get the following error:
f:cd: no such file or directory: /random
I am aware I could just use the entire directory in the alias, but this was working when I was using bash
. How do I get this to work in zsh
?
zsh alias
add a comment |Â
up vote
0
down vote
favorite
I have a directory variable and function as follows:
coding_dir="~/Documents/coding"
function f() cd $1 && ls -a ;
And I want to create an alias as follows:
alias rnd="f $coding_dir/random"
But when I try using the alias, I get the following error:
f:cd: no such file or directory: /random
I am aware I could just use the entire directory in the alias, but this was working when I was using bash
. How do I get this to work in zsh
?
zsh alias
1
You might need to firstexport coding_dir
.
â DopeGhoti
Apr 2 at 22:51
1
your code works fine with zsh 5.4.2
â don_crissti
Apr 2 at 22:52
@DopeGhoti What doesexport
do?
â Kevin
Apr 2 at 23:16
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a directory variable and function as follows:
coding_dir="~/Documents/coding"
function f() cd $1 && ls -a ;
And I want to create an alias as follows:
alias rnd="f $coding_dir/random"
But when I try using the alias, I get the following error:
f:cd: no such file or directory: /random
I am aware I could just use the entire directory in the alias, but this was working when I was using bash
. How do I get this to work in zsh
?
zsh alias
I have a directory variable and function as follows:
coding_dir="~/Documents/coding"
function f() cd $1 && ls -a ;
And I want to create an alias as follows:
alias rnd="f $coding_dir/random"
But when I try using the alias, I get the following error:
f:cd: no such file or directory: /random
I am aware I could just use the entire directory in the alias, but this was working when I was using bash
. How do I get this to work in zsh
?
zsh alias
asked Apr 2 at 22:43
Kevin
11016
11016
1
You might need to firstexport coding_dir
.
â DopeGhoti
Apr 2 at 22:51
1
your code works fine with zsh 5.4.2
â don_crissti
Apr 2 at 22:52
@DopeGhoti What doesexport
do?
â Kevin
Apr 2 at 23:16
add a comment |Â
1
You might need to firstexport coding_dir
.
â DopeGhoti
Apr 2 at 22:51
1
your code works fine with zsh 5.4.2
â don_crissti
Apr 2 at 22:52
@DopeGhoti What doesexport
do?
â Kevin
Apr 2 at 23:16
1
1
You might need to first
export coding_dir
.â DopeGhoti
Apr 2 at 22:51
You might need to first
export coding_dir
.â DopeGhoti
Apr 2 at 22:51
1
1
your code works fine with zsh 5.4.2
â don_crissti
Apr 2 at 22:52
your code works fine with zsh 5.4.2
â don_crissti
Apr 2 at 22:52
@DopeGhoti What does
export
do?â Kevin
Apr 2 at 23:16
@DopeGhoti What does
export
do?â Kevin
Apr 2 at 23:16
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Is it possible you're defining the alias before you set $coding_dir? From the error message, it looks like that's what's happening.
The issue is that you're expanding $coding_dir at the time when the alias is defined, not when it's used.
If you want it to expand it at usage time, then you should probably define it using single quotes, so that it will only expand $coding_dir when it's used.
alias rnd='f $coding_dir/random'
This will work even if you haven't defined $coding_dir yet and define it later:
$ coding_dir=~/Documents/coding
$ rnd
(should chdir to your $HOME/Documents/coding/random)
You might also want to add quotes to your alias, so that it handles directory names with spaces:
alias rnd='f "$coding_dir"/random'
And also in your definition of the f function:
function f() cd "$1" && ls -a ;
It was because I definedcoding_dir
after the alias. Simple oversight on my part. Thanks!
â Kevin
Apr 2 at 23:18
add a comment |Â
up vote
0
down vote
The problem is caused by your usage of tilde combined with quotes. See for instance this transcript from my system:
-0-1- ~ > x="~/tmp"
-0-1- ~ > cd $x
cd: no such file or directory: ~/tmp
-1-1- ~ > y="$HOME/tmp"
-0-1- ~ > cd $y
-0-1- ~/tmp > cd ..
-0-1- ~ > z=~/tmp
-0-1- ~ > cd $z
-0-1- ~/tmp >
Your tilde won't be expanded, because it is placed within quotes.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Is it possible you're defining the alias before you set $coding_dir? From the error message, it looks like that's what's happening.
The issue is that you're expanding $coding_dir at the time when the alias is defined, not when it's used.
If you want it to expand it at usage time, then you should probably define it using single quotes, so that it will only expand $coding_dir when it's used.
alias rnd='f $coding_dir/random'
This will work even if you haven't defined $coding_dir yet and define it later:
$ coding_dir=~/Documents/coding
$ rnd
(should chdir to your $HOME/Documents/coding/random)
You might also want to add quotes to your alias, so that it handles directory names with spaces:
alias rnd='f "$coding_dir"/random'
And also in your definition of the f function:
function f() cd "$1" && ls -a ;
It was because I definedcoding_dir
after the alias. Simple oversight on my part. Thanks!
â Kevin
Apr 2 at 23:18
add a comment |Â
up vote
2
down vote
accepted
Is it possible you're defining the alias before you set $coding_dir? From the error message, it looks like that's what's happening.
The issue is that you're expanding $coding_dir at the time when the alias is defined, not when it's used.
If you want it to expand it at usage time, then you should probably define it using single quotes, so that it will only expand $coding_dir when it's used.
alias rnd='f $coding_dir/random'
This will work even if you haven't defined $coding_dir yet and define it later:
$ coding_dir=~/Documents/coding
$ rnd
(should chdir to your $HOME/Documents/coding/random)
You might also want to add quotes to your alias, so that it handles directory names with spaces:
alias rnd='f "$coding_dir"/random'
And also in your definition of the f function:
function f() cd "$1" && ls -a ;
It was because I definedcoding_dir
after the alias. Simple oversight on my part. Thanks!
â Kevin
Apr 2 at 23:18
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Is it possible you're defining the alias before you set $coding_dir? From the error message, it looks like that's what's happening.
The issue is that you're expanding $coding_dir at the time when the alias is defined, not when it's used.
If you want it to expand it at usage time, then you should probably define it using single quotes, so that it will only expand $coding_dir when it's used.
alias rnd='f $coding_dir/random'
This will work even if you haven't defined $coding_dir yet and define it later:
$ coding_dir=~/Documents/coding
$ rnd
(should chdir to your $HOME/Documents/coding/random)
You might also want to add quotes to your alias, so that it handles directory names with spaces:
alias rnd='f "$coding_dir"/random'
And also in your definition of the f function:
function f() cd "$1" && ls -a ;
Is it possible you're defining the alias before you set $coding_dir? From the error message, it looks like that's what's happening.
The issue is that you're expanding $coding_dir at the time when the alias is defined, not when it's used.
If you want it to expand it at usage time, then you should probably define it using single quotes, so that it will only expand $coding_dir when it's used.
alias rnd='f $coding_dir/random'
This will work even if you haven't defined $coding_dir yet and define it later:
$ coding_dir=~/Documents/coding
$ rnd
(should chdir to your $HOME/Documents/coding/random)
You might also want to add quotes to your alias, so that it handles directory names with spaces:
alias rnd='f "$coding_dir"/random'
And also in your definition of the f function:
function f() cd "$1" && ls -a ;
answered Apr 2 at 22:51
Filipe Brandenburger
3,451621
3,451621
It was because I definedcoding_dir
after the alias. Simple oversight on my part. Thanks!
â Kevin
Apr 2 at 23:18
add a comment |Â
It was because I definedcoding_dir
after the alias. Simple oversight on my part. Thanks!
â Kevin
Apr 2 at 23:18
It was because I defined
coding_dir
after the alias. Simple oversight on my part. Thanks!â Kevin
Apr 2 at 23:18
It was because I defined
coding_dir
after the alias. Simple oversight on my part. Thanks!â Kevin
Apr 2 at 23:18
add a comment |Â
up vote
0
down vote
The problem is caused by your usage of tilde combined with quotes. See for instance this transcript from my system:
-0-1- ~ > x="~/tmp"
-0-1- ~ > cd $x
cd: no such file or directory: ~/tmp
-1-1- ~ > y="$HOME/tmp"
-0-1- ~ > cd $y
-0-1- ~/tmp > cd ..
-0-1- ~ > z=~/tmp
-0-1- ~ > cd $z
-0-1- ~/tmp >
Your tilde won't be expanded, because it is placed within quotes.
add a comment |Â
up vote
0
down vote
The problem is caused by your usage of tilde combined with quotes. See for instance this transcript from my system:
-0-1- ~ > x="~/tmp"
-0-1- ~ > cd $x
cd: no such file or directory: ~/tmp
-1-1- ~ > y="$HOME/tmp"
-0-1- ~ > cd $y
-0-1- ~/tmp > cd ..
-0-1- ~ > z=~/tmp
-0-1- ~ > cd $z
-0-1- ~/tmp >
Your tilde won't be expanded, because it is placed within quotes.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The problem is caused by your usage of tilde combined with quotes. See for instance this transcript from my system:
-0-1- ~ > x="~/tmp"
-0-1- ~ > cd $x
cd: no such file or directory: ~/tmp
-1-1- ~ > y="$HOME/tmp"
-0-1- ~ > cd $y
-0-1- ~/tmp > cd ..
-0-1- ~ > z=~/tmp
-0-1- ~ > cd $z
-0-1- ~/tmp >
Your tilde won't be expanded, because it is placed within quotes.
The problem is caused by your usage of tilde combined with quotes. See for instance this transcript from my system:
-0-1- ~ > x="~/tmp"
-0-1- ~ > cd $x
cd: no such file or directory: ~/tmp
-1-1- ~ > y="$HOME/tmp"
-0-1- ~ > cd $y
-0-1- ~/tmp > cd ..
-0-1- ~ > z=~/tmp
-0-1- ~ > cd $z
-0-1- ~/tmp >
Your tilde won't be expanded, because it is placed within quotes.
answered Apr 4 at 7:12
user1934428
35119
35119
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f435147%2fusing-dir-var-in-an-alias%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
You might need to first
export coding_dir
.â DopeGhoti
Apr 2 at 22:51
1
your code works fine with zsh 5.4.2
â don_crissti
Apr 2 at 22:52
@DopeGhoti What does
export
do?â Kevin
Apr 2 at 23:16