Difference when executing bash function in an alias









up vote
1
down vote

favorite
1












I have a function in my .bash_profile for printing text some pre-written text and copying it to the clipboard.



copyandprint () 
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "printf -- '$1'


I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.



alias shrug=$( copyandprint '¯_(ツ)_/¯')


$ shrug #=> copied ¯_(ツ)_/¯ to clipboard



But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.



alias copydate=$( copyandprint "$(date)" )


the time is from when the script was first executed, not when the alias was used



The value is generated when the script is run, not when the alias is used.



Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:



copyandprint_live () 
s='\033[1;32m' #strong
n='\033[0m' #normal
printf -- "$1"
alias copydate_live="$( copyandprint_live "$(date)" )"


date generated at time alias is used



The date is generated at the time the alias is used, rather than at the time the script is executed.



But when I use that function the way I used the other one, it fails:



alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
$ shrug_2
#=> -bash: syntax error near unexpected token `ツ'


And I tried putting double quotes, but that didn't work



alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
$ shrug_3
#=> copied 033[1
#=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory


My question is, what's going on here? Why do they need to be so different?










share|improve this question



























    up vote
    1
    down vote

    favorite
    1












    I have a function in my .bash_profile for printing text some pre-written text and copying it to the clipboard.



    copyandprint () 
    s='\033[1;32m' #strong
    n='\033[0m' #normal
    printf -- "printf -- '$1'


    I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.



    alias shrug=$( copyandprint '¯_(ツ)_/¯')


    $ shrug #=> copied ¯_(ツ)_/¯ to clipboard



    But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.



    alias copydate=$( copyandprint "$(date)" )


    the time is from when the script was first executed, not when the alias was used



    The value is generated when the script is run, not when the alias is used.



    Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:



    copyandprint_live () 
    s='\033[1;32m' #strong
    n='\033[0m' #normal
    printf -- "$1"
    alias copydate_live="$( copyandprint_live "$(date)" )"


    date generated at time alias is used



    The date is generated at the time the alias is used, rather than at the time the script is executed.



    But when I use that function the way I used the other one, it fails:



    alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
    $ shrug_2
    #=> -bash: syntax error near unexpected token `ツ'


    And I tried putting double quotes, but that didn't work



    alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
    $ shrug_3
    #=> copied 033[1
    #=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory


    My question is, what's going on here? Why do they need to be so different?










    share|improve this question

























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have a function in my .bash_profile for printing text some pre-written text and copying it to the clipboard.



      copyandprint () 
      s='\033[1;32m' #strong
      n='\033[0m' #normal
      printf -- "printf -- '$1'


      I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.



      alias shrug=$( copyandprint '¯_(ツ)_/¯')


      $ shrug #=> copied ¯_(ツ)_/¯ to clipboard



      But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.



      alias copydate=$( copyandprint "$(date)" )


      the time is from when the script was first executed, not when the alias was used



      The value is generated when the script is run, not when the alias is used.



      Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:



      copyandprint_live () 
      s='\033[1;32m' #strong
      n='\033[0m' #normal
      printf -- "$1"
      alias copydate_live="$( copyandprint_live "$(date)" )"


      date generated at time alias is used



      The date is generated at the time the alias is used, rather than at the time the script is executed.



      But when I use that function the way I used the other one, it fails:



      alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
      $ shrug_2
      #=> -bash: syntax error near unexpected token `ツ'


      And I tried putting double quotes, but that didn't work



      alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
      $ shrug_3
      #=> copied 033[1
      #=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory


      My question is, what's going on here? Why do they need to be so different?










      share|improve this question















      I have a function in my .bash_profile for printing text some pre-written text and copying it to the clipboard.



      copyandprint () 
      s='\033[1;32m' #strong
      n='\033[0m' #normal
      printf -- "printf -- '$1'


      I use this to alias things I keep wanting to paste into other applications, like static IDs, or just silly things that are difficult to type quickly on a keyboard.



      alias shrug=$( copyandprint '¯_(ツ)_/¯')


      $ shrug #=> copied ¯_(ツ)_/¯ to clipboard



      But when I wanted to use it with text generated at the time I use the alias, I can't just call it in the alias definition; the alias needs to call it.



      alias copydate=$( copyandprint "$(date)" )


      the time is from when the script was first executed, not when the alias was used



      The value is generated when the script is run, not when the alias is used.



      Through pretty much sheer trial and error, I was able to make a modified version of the function that does what I wanted:



      copyandprint_live () 
      s='\033[1;32m' #strong
      n='\033[0m' #normal
      printf -- "$1"
      alias copydate_live="$( copyandprint_live "$(date)" )"


      date generated at time alias is used



      The date is generated at the time the alias is used, rather than at the time the script is executed.



      But when I use that function the way I used the other one, it fails:



      alias shrug_2=$( copyandprint_live '¯_(ツ)_/¯')
      $ shrug_2
      #=> -bash: syntax error near unexpected token `ツ'


      And I tried putting double quotes, but that didn't work



      alias shrug_3=$( copyandprint_live '"¯_(ツ)_/¯"')
      $ shrug_3
      #=> copied 033[1
      #=> -bash: 32m¯_(ツ)_/¯033[0m: No such file or directory


      My question is, what's going on here? Why do they need to be so different?







      bash alias






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 20:36

























      asked Nov 9 at 20:31









      Nathan Hinchey

      675418




      675418






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Dispensing with the aliases and using functions makes this a lot easier.



          copyandprint () pbcopy
          printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"


          shrug ()
          copyandprint '¯_(ツ)_/¯'


          copydate ()
          copyandprint "$(date)"



          Functions work alike any other command:



          $ foo () echo hi; 
          $ foo
          hi





          share|improve this answer


















          • 1




            If you are referring to my shrug function, there is zero benefit to defining the alias shruggie like that; you're just forking an extra shell to run something that can run in your current shell.
            – chepner
            Nov 9 at 21:05










          • The comment chepner is replying to above said: "You can also define an alias using that: alias shruggie="$(shrug)""
            – Nathan Hinchey
            Nov 9 at 21:18











          • But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
            – Nathan Hinchey
            Nov 9 at 21:19







          • 1




            A function is just a named command. You don't need an alias to refer to it; you just use it.
            – chepner
            Nov 9 at 21:20






          • 1




            I redefined copyandprint to actually do the copying and printing, rather than generating the body of an alias.
            – chepner
            Nov 9 at 21:24

















          up vote
          0
          down vote













          You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...) from executing the command at that time.



          alias shrug='$( copyandprint "¯_(ツ)_/¯")'





          share|improve this answer




















            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',
            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%2f53232877%2fdifference-when-executing-bash-function-in-an-alias%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








            up vote
            2
            down vote



            accepted










            Dispensing with the aliases and using functions makes this a lot easier.



            copyandprint () pbcopy
            printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"


            shrug ()
            copyandprint '¯_(ツ)_/¯'


            copydate ()
            copyandprint "$(date)"



            Functions work alike any other command:



            $ foo () echo hi; 
            $ foo
            hi





            share|improve this answer


















            • 1




              If you are referring to my shrug function, there is zero benefit to defining the alias shruggie like that; you're just forking an extra shell to run something that can run in your current shell.
              – chepner
              Nov 9 at 21:05










            • The comment chepner is replying to above said: "You can also define an alias using that: alias shruggie="$(shrug)""
              – Nathan Hinchey
              Nov 9 at 21:18











            • But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
              – Nathan Hinchey
              Nov 9 at 21:19







            • 1




              A function is just a named command. You don't need an alias to refer to it; you just use it.
              – chepner
              Nov 9 at 21:20






            • 1




              I redefined copyandprint to actually do the copying and printing, rather than generating the body of an alias.
              – chepner
              Nov 9 at 21:24














            up vote
            2
            down vote



            accepted










            Dispensing with the aliases and using functions makes this a lot easier.



            copyandprint () pbcopy
            printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"


            shrug ()
            copyandprint '¯_(ツ)_/¯'


            copydate ()
            copyandprint "$(date)"



            Functions work alike any other command:



            $ foo () echo hi; 
            $ foo
            hi





            share|improve this answer


















            • 1




              If you are referring to my shrug function, there is zero benefit to defining the alias shruggie like that; you're just forking an extra shell to run something that can run in your current shell.
              – chepner
              Nov 9 at 21:05










            • The comment chepner is replying to above said: "You can also define an alias using that: alias shruggie="$(shrug)""
              – Nathan Hinchey
              Nov 9 at 21:18











            • But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
              – Nathan Hinchey
              Nov 9 at 21:19







            • 1




              A function is just a named command. You don't need an alias to refer to it; you just use it.
              – chepner
              Nov 9 at 21:20






            • 1




              I redefined copyandprint to actually do the copying and printing, rather than generating the body of an alias.
              – chepner
              Nov 9 at 21:24












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Dispensing with the aliases and using functions makes this a lot easier.



            copyandprint () pbcopy
            printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"


            shrug ()
            copyandprint '¯_(ツ)_/¯'


            copydate ()
            copyandprint "$(date)"



            Functions work alike any other command:



            $ foo () echo hi; 
            $ foo
            hi





            share|improve this answer














            Dispensing with the aliases and using functions makes this a lot easier.



            copyandprint () pbcopy
            printf 'copied 33[1;32m%s33[0m to clipboardn' "$1"


            shrug ()
            copyandprint '¯_(ツ)_/¯'


            copydate ()
            copyandprint "$(date)"



            Functions work alike any other command:



            $ foo () echo hi; 
            $ foo
            hi






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 21:20

























            answered Nov 9 at 20:39









            chepner

            239k29225319




            239k29225319







            • 1




              If you are referring to my shrug function, there is zero benefit to defining the alias shruggie like that; you're just forking an extra shell to run something that can run in your current shell.
              – chepner
              Nov 9 at 21:05










            • The comment chepner is replying to above said: "You can also define an alias using that: alias shruggie="$(shrug)""
              – Nathan Hinchey
              Nov 9 at 21:18











            • But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
              – Nathan Hinchey
              Nov 9 at 21:19







            • 1




              A function is just a named command. You don't need an alias to refer to it; you just use it.
              – chepner
              Nov 9 at 21:20






            • 1




              I redefined copyandprint to actually do the copying and printing, rather than generating the body of an alias.
              – chepner
              Nov 9 at 21:24












            • 1




              If you are referring to my shrug function, there is zero benefit to defining the alias shruggie like that; you're just forking an extra shell to run something that can run in your current shell.
              – chepner
              Nov 9 at 21:05










            • The comment chepner is replying to above said: "You can also define an alias using that: alias shruggie="$(shrug)""
              – Nathan Hinchey
              Nov 9 at 21:18











            • But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
              – Nathan Hinchey
              Nov 9 at 21:19







            • 1




              A function is just a named command. You don't need an alias to refer to it; you just use it.
              – chepner
              Nov 9 at 21:20






            • 1




              I redefined copyandprint to actually do the copying and printing, rather than generating the body of an alias.
              – chepner
              Nov 9 at 21:24







            1




            1




            If you are referring to my shrug function, there is zero benefit to defining the alias shruggie like that; you're just forking an extra shell to run something that can run in your current shell.
            – chepner
            Nov 9 at 21:05




            If you are referring to my shrug function, there is zero benefit to defining the alias shruggie like that; you're just forking an extra shell to run something that can run in your current shell.
            – chepner
            Nov 9 at 21:05












            The comment chepner is replying to above said: "You can also define an alias using that: alias shruggie="$(shrug)""
            – Nathan Hinchey
            Nov 9 at 21:18





            The comment chepner is replying to above said: "You can also define an alias using that: alias shruggie="$(shrug)""
            – Nathan Hinchey
            Nov 9 at 21:18













            But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
            – Nathan Hinchey
            Nov 9 at 21:19





            But how do I use it in the terminal without defining it as an alias? The whole point of this was to have terminal aliases
            – Nathan Hinchey
            Nov 9 at 21:19





            1




            1




            A function is just a named command. You don't need an alias to refer to it; you just use it.
            – chepner
            Nov 9 at 21:20




            A function is just a named command. You don't need an alias to refer to it; you just use it.
            – chepner
            Nov 9 at 21:20




            1




            1




            I redefined copyandprint to actually do the copying and printing, rather than generating the body of an alias.
            – chepner
            Nov 9 at 21:24




            I redefined copyandprint to actually do the copying and printing, rather than generating the body of an alias.
            – chepner
            Nov 9 at 21:24












            up vote
            0
            down vote













            You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...) from executing the command at that time.



            alias shrug='$( copyandprint "¯_(ツ)_/¯")'





            share|improve this answer
























              up vote
              0
              down vote













              You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...) from executing the command at that time.



              alias shrug='$( copyandprint "¯_(ツ)_/¯")'





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...) from executing the command at that time.



                alias shrug='$( copyandprint "¯_(ツ)_/¯")'





                share|improve this answer












                You're calling the function when you define the aliases, not when you use them. You need to put the alias definition in single quotes to prevent $(...) from executing the command at that time.



                alias shrug='$( copyandprint "¯_(ツ)_/¯")'






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 20:42









                Barmar

                413k34238339




                413k34238339



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232877%2fdifference-when-executing-bash-function-in-an-alias%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







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo