How to set part of a path dynamically for alias command in bashrc file










0















I'm trying to have a alias command and the part of its path keeps changing.
Eg:



/var/mywork/swag/wsnew/
/var/mywork/swag/ws/
/var/mywork/swag/wsold/


and my alias command to achieve is something link this



alias cws='cd /var/mywork/swag//



since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?



I tried something like



alias cws='cd /var/mywork/swag/getenv("WSP")/



so whenever I set WSP to required path it automatically takes. But it didn't help.










share|improve this question




























    0















    I'm trying to have a alias command and the part of its path keeps changing.
    Eg:



    /var/mywork/swag/wsnew/
    /var/mywork/swag/ws/
    /var/mywork/swag/wsold/


    and my alias command to achieve is something link this



    alias cws='cd /var/mywork/swag//



    since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?



    I tried something like



    alias cws='cd /var/mywork/swag/getenv("WSP")/



    so whenever I set WSP to required path it automatically takes. But it didn't help.










    share|improve this question


























      0












      0








      0








      I'm trying to have a alias command and the part of its path keeps changing.
      Eg:



      /var/mywork/swag/wsnew/
      /var/mywork/swag/ws/
      /var/mywork/swag/wsold/


      and my alias command to achieve is something link this



      alias cws='cd /var/mywork/swag//



      since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?



      I tried something like



      alias cws='cd /var/mywork/swag/getenv("WSP")/



      so whenever I set WSP to required path it automatically takes. But it didn't help.










      share|improve this question
















      I'm trying to have a alias command and the part of its path keeps changing.
      Eg:



      /var/mywork/swag/wsnew/
      /var/mywork/swag/ws/
      /var/mywork/swag/wsold/


      and my alias command to achieve is something link this



      alias cws='cd /var/mywork/swag//



      since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?



      I tried something like



      alias cws='cd /var/mywork/swag/getenv("WSP")/



      so whenever I set WSP to required path it automatically takes. But it didn't help.







      bash shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 7:31









      melpomene

      61.6k54994




      61.6k54994










      asked Nov 15 '18 at 7:07









      KhrusosKhrusos

      275




      275






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Like most problems with aliases, this is easier to solve with a shell function:



          cws() 
          cd /var/mywork/swag/"$WSP"






          share|improve this answer






























            0














            Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do



            alias cws='cd /var/mywork/swag/$WSP'


            But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write



            WSP=aaa
            ...
            WSP=xxx
            ...
            WSP=yyy cws


            and cws is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.






            share|improve this answer























            • Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on: alias cws="cd /var/mywork/swag/$WSP" Quoting the $ makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)

              – Paul Hodges
              Nov 15 '18 at 14:33







            • 1





              Sure, a function with an argument such as $1:-$WSP:-some_nice_default would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag and get rid of alias and function.

              – user1934428
              Nov 16 '18 at 7:52










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314097%2fhow-to-set-part-of-a-path-dynamically-for-alias-command-in-bashrc-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Like most problems with aliases, this is easier to solve with a shell function:



            cws() 
            cd /var/mywork/swag/"$WSP"






            share|improve this answer



























              1














              Like most problems with aliases, this is easier to solve with a shell function:



              cws() 
              cd /var/mywork/swag/"$WSP"






              share|improve this answer

























                1












                1








                1







                Like most problems with aliases, this is easier to solve with a shell function:



                cws() 
                cd /var/mywork/swag/"$WSP"






                share|improve this answer













                Like most problems with aliases, this is easier to solve with a shell function:



                cws() 
                cd /var/mywork/swag/"$WSP"







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 7:30









                melpomenemelpomene

                61.6k54994




                61.6k54994























                    0














                    Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do



                    alias cws='cd /var/mywork/swag/$WSP'


                    But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write



                    WSP=aaa
                    ...
                    WSP=xxx
                    ...
                    WSP=yyy cws


                    and cws is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.






                    share|improve this answer























                    • Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on: alias cws="cd /var/mywork/swag/$WSP" Quoting the $ makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)

                      – Paul Hodges
                      Nov 15 '18 at 14:33







                    • 1





                      Sure, a function with an argument such as $1:-$WSP:-some_nice_default would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag and get rid of alias and function.

                      – user1934428
                      Nov 16 '18 at 7:52















                    0














                    Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do



                    alias cws='cd /var/mywork/swag/$WSP'


                    But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write



                    WSP=aaa
                    ...
                    WSP=xxx
                    ...
                    WSP=yyy cws


                    and cws is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.






                    share|improve this answer























                    • Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on: alias cws="cd /var/mywork/swag/$WSP" Quoting the $ makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)

                      – Paul Hodges
                      Nov 15 '18 at 14:33







                    • 1





                      Sure, a function with an argument such as $1:-$WSP:-some_nice_default would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag and get rid of alias and function.

                      – user1934428
                      Nov 16 '18 at 7:52













                    0












                    0








                    0







                    Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do



                    alias cws='cd /var/mywork/swag/$WSP'


                    But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write



                    WSP=aaa
                    ...
                    WSP=xxx
                    ...
                    WSP=yyy cws


                    and cws is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.






                    share|improve this answer













                    Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do



                    alias cws='cd /var/mywork/swag/$WSP'


                    But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write



                    WSP=aaa
                    ...
                    WSP=xxx
                    ...
                    WSP=yyy cws


                    and cws is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 7:45









                    user1934428user1934428

                    4,17221633




                    4,17221633












                    • Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on: alias cws="cd /var/mywork/swag/$WSP" Quoting the $ makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)

                      – Paul Hodges
                      Nov 15 '18 at 14:33







                    • 1





                      Sure, a function with an argument such as $1:-$WSP:-some_nice_default would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag and get rid of alias and function.

                      – user1934428
                      Nov 16 '18 at 7:52

















                    • Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on: alias cws="cd /var/mywork/swag/$WSP" Quoting the $ makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)

                      – Paul Hodges
                      Nov 15 '18 at 14:33







                    • 1





                      Sure, a function with an argument such as $1:-$WSP:-some_nice_default would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag and get rid of alias and function.

                      – user1934428
                      Nov 16 '18 at 7:52
















                    Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on: alias cws="cd /var/mywork/swag/$WSP" Quoting the $ makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)

                    – Paul Hodges
                    Nov 15 '18 at 14:33






                    Yep. Same result with slightly different syntax, just in the possibility that variety makes it a little easier to grasp what's going on: alias cws="cd /var/mywork/swag/$WSP" Quoting the $ makes it evaluate at execution time rather than once only when set, but wouldn't it be easier to have a function that could take an argument? Listen to the Muse. :)

                    – Paul Hodges
                    Nov 15 '18 at 14:33





                    1




                    1





                    Sure, a function with an argument such as $1:-$WSP:-some_nice_default would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag and get rid of alias and function.

                    – user1934428
                    Nov 16 '18 at 7:52





                    Sure, a function with an argument such as $1:-$WSP:-some_nice_default would be more flexible, and this is what I would do in this case; if I understand the OP correctly, he doesn't intend to change WSP often, so his function would usually be invoked without arguments. Another option to consider would be to set CDPATH=.:/var/mywork/swag and get rid of alias and function.

                    – user1934428
                    Nov 16 '18 at 7:52

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314097%2fhow-to-set-part-of-a-path-dynamically-for-alias-command-in-bashrc-file%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto