Using dir var in an alias?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question
















  • 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














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?







share|improve this question
















  • 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












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?







share|improve this question












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?









share|improve this question











share|improve this question




share|improve this question










asked Apr 2 at 22:43









Kevin

11016




11016







  • 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












  • 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







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










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 ; 





share|improve this answer




















  • It was because I defined coding_dir after the alias. Simple oversight on my part. Thanks!
    – Kevin
    Apr 2 at 23:18

















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.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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 ; 





    share|improve this answer




















    • It was because I defined coding_dir after the alias. Simple oversight on my part. Thanks!
      – Kevin
      Apr 2 at 23:18














    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 ; 





    share|improve this answer




















    • It was because I defined coding_dir after the alias. Simple oversight on my part. Thanks!
      – Kevin
      Apr 2 at 23:18












    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 ; 





    share|improve this answer












    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 ; 






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 2 at 22:51









    Filipe Brandenburger

    3,451621




    3,451621











    • 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















    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












    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 4 at 7:12









        user1934428

        35119




        35119






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay