Where can I put a user-defined shell function?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







13















I am planning to create a function that would simplify things for me. The function would be something like



function lazymode()
{
echo "Hello World!";
}


so that when I use the command lazymode in the shell , it will output the Hello World!.



What file should I put the user-defined function?










share|improve this question































    13















    I am planning to create a function that would simplify things for me. The function would be something like



    function lazymode()
    {
    echo "Hello World!";
    }


    so that when I use the command lazymode in the shell , it will output the Hello World!.



    What file should I put the user-defined function?










    share|improve this question



























      13












      13








      13


      4






      I am planning to create a function that would simplify things for me. The function would be something like



      function lazymode()
      {
      echo "Hello World!";
      }


      so that when I use the command lazymode in the shell , it will output the Hello World!.



      What file should I put the user-defined function?










      share|improve this question
















      I am planning to create a function that would simplify things for me. The function would be something like



      function lazymode()
      {
      echo "Hello World!";
      }


      so that when I use the command lazymode in the shell , it will output the Hello World!.



      What file should I put the user-defined function?







      command-line bash functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 1 '16 at 9:20









      muru

      1




      1










      asked Sep 12 '13 at 8:09









      Abel Melquiades CallejoAbel Melquiades Callejo

      5201617




      5201617






















          3 Answers
          3






          active

          oldest

          votes


















          12














          Depends on the function. If it's just a super-simple one-liner like that you could create an alias or stick the function in ~/.bashrc (a file that bash loads when it starts).



          If you're creating something a bit more meaty, it might make more sense to create its own executable script in ~/bin/ which won't exist by default (it's just a directory) but should be in your path. Remember for this the file will need to be executable (chmod +x filename) and start with a proper #!/bin/bash stanza.



          The second route has some clear benefits:




          • It's easier to see what's available

          • A syntax error won't tank your profile

          • You don't need to keep re-sourcing your bash config if you change the script

          • It's available to any shell as long as the full path is used or ~/bin/ is in the path for that shell too (which it should be in most cases AFAIK).






          share|improve this answer


























          • I used the later option. My function contains commands that delete files, wget's something, executes the commands javac and java. and when I tried it on the gnome-terminal, it's not working fine. Do you think there is an issue?

            – Abel Melquiades Callejo
            Sep 12 '13 at 9:14













          • What does it do instead?

            – Oli
            Sep 12 '13 at 9:37











          • It's already fixed. Fixed through this answer

            – Abel Melquiades Callejo
            Sep 13 '13 at 17:02











          • umm... shouldn't user scripts be in the /usr/local/bin instead messing with the $PATH?

            – Braiam
            Sep 15 '13 at 2:28











          • It seems to me that a small but worth-mentioning advantage of putting functions in ~/.bashrc (as it is sourced) is that Bash looks for functions before doing PATH lookup.

            – Zanna
            Sep 27 '18 at 19:42



















          4














          The best choice would be ~/.bashrc file.



          You can either write your shell function definitions directly in your ~/.bashrc file, or, if you have lots of them and don't want to clutter your ~/.bashrc file, you can put them all in another file of your choosing -- just be sure to source that file in your ~/.bashrc file. For example, if the file with your functions is named bash_functions, simply add in your ~/.bashrc file the lines:



          if [[ -f /path/to/bash_functions ]]; then
          source /path/to/bash_functions
          fi


          or, equivalently:



          if [[ -f /path/to/bash_functions ]]; then
          . /path/to/bash_functions
          fi


          where the . is just a symbolic representation of source. The if test makes sure the file /path/to/bash_functions exists before trying to source it.



          This technique is very similar to establishing aliases in ~/.bashrc by creating a file called ~/.bash_aliases and using similar syntax to that above in ~/.bashrc to test for its existence and then source it.






          share|improve this answer


























          • thanks, it worked. by the way, what is its difference with .profile? in some linux, I cannot put functions in bashrc

            – Abel Melquiades Callejo
            Sep 12 '13 at 8:15






          • 2





            ~/.profile file is executed by the command interpreter for login shells. When you use GUI and you open the terminal, that file is not executed because you will be in a not login shell.

            – Radu Rădeanu
            Sep 12 '13 at 8:18



















          1














          Here's an essential procedure for declaring a permanent function:




          1. Open ~/.bashrc file in a text editor. Doesn't matter which text editor, so long as you know how to use it and so long as you open the /home/<username>/.bashrc



          2. At the end of the ~/.bashrc declare your own function, for instance:



            find_dirs(){
            find "$1" -type d
            }


          3. Save and close the file.



          The ~/.bashrc file is read every time you open interactive shell (that is new terminal tab, login via ssh, or open TTY1 or other virtual console). This will not be available in script files, because ~/.bashrc is not read for non-interactive shells. It is also not available if you run bash with --norc option.



          If you want the function to be available immediately in the currently open tab, use source ~/.bashrc command.





          Functions take arguments just like regular commands. For example, $1 through $9 indicate the positional parameters when you call a function. In the example above find_dirs takes one positional parameter only, and would be called as find_dirs /etc. You can also use $@ to refer to all positional parameters. Functions also accept redirection. You can call a function with find_dirs $1 > /dev/null; we also could declare it as follows:



          find_dirs(){
          find "$1" -type d
          }


          Note from man bash: "Functions are executed in the context of the current shell; no new process is created to interpret them". That means that you also should be aware of functions having ability to alter your shell execution environment - change variables and terminal settings.






          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            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%2faskubuntu.com%2fquestions%2f344557%2fwhere-can-i-put-a-user-defined-shell-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            12














            Depends on the function. If it's just a super-simple one-liner like that you could create an alias or stick the function in ~/.bashrc (a file that bash loads when it starts).



            If you're creating something a bit more meaty, it might make more sense to create its own executable script in ~/bin/ which won't exist by default (it's just a directory) but should be in your path. Remember for this the file will need to be executable (chmod +x filename) and start with a proper #!/bin/bash stanza.



            The second route has some clear benefits:




            • It's easier to see what's available

            • A syntax error won't tank your profile

            • You don't need to keep re-sourcing your bash config if you change the script

            • It's available to any shell as long as the full path is used or ~/bin/ is in the path for that shell too (which it should be in most cases AFAIK).






            share|improve this answer


























            • I used the later option. My function contains commands that delete files, wget's something, executes the commands javac and java. and when I tried it on the gnome-terminal, it's not working fine. Do you think there is an issue?

              – Abel Melquiades Callejo
              Sep 12 '13 at 9:14













            • What does it do instead?

              – Oli
              Sep 12 '13 at 9:37











            • It's already fixed. Fixed through this answer

              – Abel Melquiades Callejo
              Sep 13 '13 at 17:02











            • umm... shouldn't user scripts be in the /usr/local/bin instead messing with the $PATH?

              – Braiam
              Sep 15 '13 at 2:28











            • It seems to me that a small but worth-mentioning advantage of putting functions in ~/.bashrc (as it is sourced) is that Bash looks for functions before doing PATH lookup.

              – Zanna
              Sep 27 '18 at 19:42
















            12














            Depends on the function. If it's just a super-simple one-liner like that you could create an alias or stick the function in ~/.bashrc (a file that bash loads when it starts).



            If you're creating something a bit more meaty, it might make more sense to create its own executable script in ~/bin/ which won't exist by default (it's just a directory) but should be in your path. Remember for this the file will need to be executable (chmod +x filename) and start with a proper #!/bin/bash stanza.



            The second route has some clear benefits:




            • It's easier to see what's available

            • A syntax error won't tank your profile

            • You don't need to keep re-sourcing your bash config if you change the script

            • It's available to any shell as long as the full path is used or ~/bin/ is in the path for that shell too (which it should be in most cases AFAIK).






            share|improve this answer


























            • I used the later option. My function contains commands that delete files, wget's something, executes the commands javac and java. and when I tried it on the gnome-terminal, it's not working fine. Do you think there is an issue?

              – Abel Melquiades Callejo
              Sep 12 '13 at 9:14













            • What does it do instead?

              – Oli
              Sep 12 '13 at 9:37











            • It's already fixed. Fixed through this answer

              – Abel Melquiades Callejo
              Sep 13 '13 at 17:02











            • umm... shouldn't user scripts be in the /usr/local/bin instead messing with the $PATH?

              – Braiam
              Sep 15 '13 at 2:28











            • It seems to me that a small but worth-mentioning advantage of putting functions in ~/.bashrc (as it is sourced) is that Bash looks for functions before doing PATH lookup.

              – Zanna
              Sep 27 '18 at 19:42














            12












            12








            12







            Depends on the function. If it's just a super-simple one-liner like that you could create an alias or stick the function in ~/.bashrc (a file that bash loads when it starts).



            If you're creating something a bit more meaty, it might make more sense to create its own executable script in ~/bin/ which won't exist by default (it's just a directory) but should be in your path. Remember for this the file will need to be executable (chmod +x filename) and start with a proper #!/bin/bash stanza.



            The second route has some clear benefits:




            • It's easier to see what's available

            • A syntax error won't tank your profile

            • You don't need to keep re-sourcing your bash config if you change the script

            • It's available to any shell as long as the full path is used or ~/bin/ is in the path for that shell too (which it should be in most cases AFAIK).






            share|improve this answer















            Depends on the function. If it's just a super-simple one-liner like that you could create an alias or stick the function in ~/.bashrc (a file that bash loads when it starts).



            If you're creating something a bit more meaty, it might make more sense to create its own executable script in ~/bin/ which won't exist by default (it's just a directory) but should be in your path. Remember for this the file will need to be executable (chmod +x filename) and start with a proper #!/bin/bash stanza.



            The second route has some clear benefits:




            • It's easier to see what's available

            • A syntax error won't tank your profile

            • You don't need to keep re-sourcing your bash config if you change the script

            • It's available to any shell as long as the full path is used or ~/bin/ is in the path for that shell too (which it should be in most cases AFAIK).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 12 '13 at 8:39

























            answered Sep 12 '13 at 8:13









            OliOli

            224k90567767




            224k90567767













            • I used the later option. My function contains commands that delete files, wget's something, executes the commands javac and java. and when I tried it on the gnome-terminal, it's not working fine. Do you think there is an issue?

              – Abel Melquiades Callejo
              Sep 12 '13 at 9:14













            • What does it do instead?

              – Oli
              Sep 12 '13 at 9:37











            • It's already fixed. Fixed through this answer

              – Abel Melquiades Callejo
              Sep 13 '13 at 17:02











            • umm... shouldn't user scripts be in the /usr/local/bin instead messing with the $PATH?

              – Braiam
              Sep 15 '13 at 2:28











            • It seems to me that a small but worth-mentioning advantage of putting functions in ~/.bashrc (as it is sourced) is that Bash looks for functions before doing PATH lookup.

              – Zanna
              Sep 27 '18 at 19:42



















            • I used the later option. My function contains commands that delete files, wget's something, executes the commands javac and java. and when I tried it on the gnome-terminal, it's not working fine. Do you think there is an issue?

              – Abel Melquiades Callejo
              Sep 12 '13 at 9:14













            • What does it do instead?

              – Oli
              Sep 12 '13 at 9:37











            • It's already fixed. Fixed through this answer

              – Abel Melquiades Callejo
              Sep 13 '13 at 17:02











            • umm... shouldn't user scripts be in the /usr/local/bin instead messing with the $PATH?

              – Braiam
              Sep 15 '13 at 2:28











            • It seems to me that a small but worth-mentioning advantage of putting functions in ~/.bashrc (as it is sourced) is that Bash looks for functions before doing PATH lookup.

              – Zanna
              Sep 27 '18 at 19:42

















            I used the later option. My function contains commands that delete files, wget's something, executes the commands javac and java. and when I tried it on the gnome-terminal, it's not working fine. Do you think there is an issue?

            – Abel Melquiades Callejo
            Sep 12 '13 at 9:14







            I used the later option. My function contains commands that delete files, wget's something, executes the commands javac and java. and when I tried it on the gnome-terminal, it's not working fine. Do you think there is an issue?

            – Abel Melquiades Callejo
            Sep 12 '13 at 9:14















            What does it do instead?

            – Oli
            Sep 12 '13 at 9:37





            What does it do instead?

            – Oli
            Sep 12 '13 at 9:37













            It's already fixed. Fixed through this answer

            – Abel Melquiades Callejo
            Sep 13 '13 at 17:02





            It's already fixed. Fixed through this answer

            – Abel Melquiades Callejo
            Sep 13 '13 at 17:02













            umm... shouldn't user scripts be in the /usr/local/bin instead messing with the $PATH?

            – Braiam
            Sep 15 '13 at 2:28





            umm... shouldn't user scripts be in the /usr/local/bin instead messing with the $PATH?

            – Braiam
            Sep 15 '13 at 2:28













            It seems to me that a small but worth-mentioning advantage of putting functions in ~/.bashrc (as it is sourced) is that Bash looks for functions before doing PATH lookup.

            – Zanna
            Sep 27 '18 at 19:42





            It seems to me that a small but worth-mentioning advantage of putting functions in ~/.bashrc (as it is sourced) is that Bash looks for functions before doing PATH lookup.

            – Zanna
            Sep 27 '18 at 19:42













            4














            The best choice would be ~/.bashrc file.



            You can either write your shell function definitions directly in your ~/.bashrc file, or, if you have lots of them and don't want to clutter your ~/.bashrc file, you can put them all in another file of your choosing -- just be sure to source that file in your ~/.bashrc file. For example, if the file with your functions is named bash_functions, simply add in your ~/.bashrc file the lines:



            if [[ -f /path/to/bash_functions ]]; then
            source /path/to/bash_functions
            fi


            or, equivalently:



            if [[ -f /path/to/bash_functions ]]; then
            . /path/to/bash_functions
            fi


            where the . is just a symbolic representation of source. The if test makes sure the file /path/to/bash_functions exists before trying to source it.



            This technique is very similar to establishing aliases in ~/.bashrc by creating a file called ~/.bash_aliases and using similar syntax to that above in ~/.bashrc to test for its existence and then source it.






            share|improve this answer


























            • thanks, it worked. by the way, what is its difference with .profile? in some linux, I cannot put functions in bashrc

              – Abel Melquiades Callejo
              Sep 12 '13 at 8:15






            • 2





              ~/.profile file is executed by the command interpreter for login shells. When you use GUI and you open the terminal, that file is not executed because you will be in a not login shell.

              – Radu Rădeanu
              Sep 12 '13 at 8:18
















            4














            The best choice would be ~/.bashrc file.



            You can either write your shell function definitions directly in your ~/.bashrc file, or, if you have lots of them and don't want to clutter your ~/.bashrc file, you can put them all in another file of your choosing -- just be sure to source that file in your ~/.bashrc file. For example, if the file with your functions is named bash_functions, simply add in your ~/.bashrc file the lines:



            if [[ -f /path/to/bash_functions ]]; then
            source /path/to/bash_functions
            fi


            or, equivalently:



            if [[ -f /path/to/bash_functions ]]; then
            . /path/to/bash_functions
            fi


            where the . is just a symbolic representation of source. The if test makes sure the file /path/to/bash_functions exists before trying to source it.



            This technique is very similar to establishing aliases in ~/.bashrc by creating a file called ~/.bash_aliases and using similar syntax to that above in ~/.bashrc to test for its existence and then source it.






            share|improve this answer


























            • thanks, it worked. by the way, what is its difference with .profile? in some linux, I cannot put functions in bashrc

              – Abel Melquiades Callejo
              Sep 12 '13 at 8:15






            • 2





              ~/.profile file is executed by the command interpreter for login shells. When you use GUI and you open the terminal, that file is not executed because you will be in a not login shell.

              – Radu Rădeanu
              Sep 12 '13 at 8:18














            4












            4








            4







            The best choice would be ~/.bashrc file.



            You can either write your shell function definitions directly in your ~/.bashrc file, or, if you have lots of them and don't want to clutter your ~/.bashrc file, you can put them all in another file of your choosing -- just be sure to source that file in your ~/.bashrc file. For example, if the file with your functions is named bash_functions, simply add in your ~/.bashrc file the lines:



            if [[ -f /path/to/bash_functions ]]; then
            source /path/to/bash_functions
            fi


            or, equivalently:



            if [[ -f /path/to/bash_functions ]]; then
            . /path/to/bash_functions
            fi


            where the . is just a symbolic representation of source. The if test makes sure the file /path/to/bash_functions exists before trying to source it.



            This technique is very similar to establishing aliases in ~/.bashrc by creating a file called ~/.bash_aliases and using similar syntax to that above in ~/.bashrc to test for its existence and then source it.






            share|improve this answer















            The best choice would be ~/.bashrc file.



            You can either write your shell function definitions directly in your ~/.bashrc file, or, if you have lots of them and don't want to clutter your ~/.bashrc file, you can put them all in another file of your choosing -- just be sure to source that file in your ~/.bashrc file. For example, if the file with your functions is named bash_functions, simply add in your ~/.bashrc file the lines:



            if [[ -f /path/to/bash_functions ]]; then
            source /path/to/bash_functions
            fi


            or, equivalently:



            if [[ -f /path/to/bash_functions ]]; then
            . /path/to/bash_functions
            fi


            where the . is just a symbolic representation of source. The if test makes sure the file /path/to/bash_functions exists before trying to source it.



            This technique is very similar to establishing aliases in ~/.bashrc by creating a file called ~/.bash_aliases and using similar syntax to that above in ~/.bashrc to test for its existence and then source it.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 1 '16 at 9:13









            quest

            1691111




            1691111










            answered Sep 12 '13 at 8:10









            Radu RădeanuRadu Rădeanu

            120k35253328




            120k35253328













            • thanks, it worked. by the way, what is its difference with .profile? in some linux, I cannot put functions in bashrc

              – Abel Melquiades Callejo
              Sep 12 '13 at 8:15






            • 2





              ~/.profile file is executed by the command interpreter for login shells. When you use GUI and you open the terminal, that file is not executed because you will be in a not login shell.

              – Radu Rădeanu
              Sep 12 '13 at 8:18



















            • thanks, it worked. by the way, what is its difference with .profile? in some linux, I cannot put functions in bashrc

              – Abel Melquiades Callejo
              Sep 12 '13 at 8:15






            • 2





              ~/.profile file is executed by the command interpreter for login shells. When you use GUI and you open the terminal, that file is not executed because you will be in a not login shell.

              – Radu Rădeanu
              Sep 12 '13 at 8:18

















            thanks, it worked. by the way, what is its difference with .profile? in some linux, I cannot put functions in bashrc

            – Abel Melquiades Callejo
            Sep 12 '13 at 8:15





            thanks, it worked. by the way, what is its difference with .profile? in some linux, I cannot put functions in bashrc

            – Abel Melquiades Callejo
            Sep 12 '13 at 8:15




            2




            2





            ~/.profile file is executed by the command interpreter for login shells. When you use GUI and you open the terminal, that file is not executed because you will be in a not login shell.

            – Radu Rădeanu
            Sep 12 '13 at 8:18





            ~/.profile file is executed by the command interpreter for login shells. When you use GUI and you open the terminal, that file is not executed because you will be in a not login shell.

            – Radu Rădeanu
            Sep 12 '13 at 8:18











            1














            Here's an essential procedure for declaring a permanent function:




            1. Open ~/.bashrc file in a text editor. Doesn't matter which text editor, so long as you know how to use it and so long as you open the /home/<username>/.bashrc



            2. At the end of the ~/.bashrc declare your own function, for instance:



              find_dirs(){
              find "$1" -type d
              }


            3. Save and close the file.



            The ~/.bashrc file is read every time you open interactive shell (that is new terminal tab, login via ssh, or open TTY1 or other virtual console). This will not be available in script files, because ~/.bashrc is not read for non-interactive shells. It is also not available if you run bash with --norc option.



            If you want the function to be available immediately in the currently open tab, use source ~/.bashrc command.





            Functions take arguments just like regular commands. For example, $1 through $9 indicate the positional parameters when you call a function. In the example above find_dirs takes one positional parameter only, and would be called as find_dirs /etc. You can also use $@ to refer to all positional parameters. Functions also accept redirection. You can call a function with find_dirs $1 > /dev/null; we also could declare it as follows:



            find_dirs(){
            find "$1" -type d
            }


            Note from man bash: "Functions are executed in the context of the current shell; no new process is created to interpret them". That means that you also should be aware of functions having ability to alter your shell execution environment - change variables and terminal settings.






            share|improve this answer




























              1














              Here's an essential procedure for declaring a permanent function:




              1. Open ~/.bashrc file in a text editor. Doesn't matter which text editor, so long as you know how to use it and so long as you open the /home/<username>/.bashrc



              2. At the end of the ~/.bashrc declare your own function, for instance:



                find_dirs(){
                find "$1" -type d
                }


              3. Save and close the file.



              The ~/.bashrc file is read every time you open interactive shell (that is new terminal tab, login via ssh, or open TTY1 or other virtual console). This will not be available in script files, because ~/.bashrc is not read for non-interactive shells. It is also not available if you run bash with --norc option.



              If you want the function to be available immediately in the currently open tab, use source ~/.bashrc command.





              Functions take arguments just like regular commands. For example, $1 through $9 indicate the positional parameters when you call a function. In the example above find_dirs takes one positional parameter only, and would be called as find_dirs /etc. You can also use $@ to refer to all positional parameters. Functions also accept redirection. You can call a function with find_dirs $1 > /dev/null; we also could declare it as follows:



              find_dirs(){
              find "$1" -type d
              }


              Note from man bash: "Functions are executed in the context of the current shell; no new process is created to interpret them". That means that you also should be aware of functions having ability to alter your shell execution environment - change variables and terminal settings.






              share|improve this answer


























                1












                1








                1







                Here's an essential procedure for declaring a permanent function:




                1. Open ~/.bashrc file in a text editor. Doesn't matter which text editor, so long as you know how to use it and so long as you open the /home/<username>/.bashrc



                2. At the end of the ~/.bashrc declare your own function, for instance:



                  find_dirs(){
                  find "$1" -type d
                  }


                3. Save and close the file.



                The ~/.bashrc file is read every time you open interactive shell (that is new terminal tab, login via ssh, or open TTY1 or other virtual console). This will not be available in script files, because ~/.bashrc is not read for non-interactive shells. It is also not available if you run bash with --norc option.



                If you want the function to be available immediately in the currently open tab, use source ~/.bashrc command.





                Functions take arguments just like regular commands. For example, $1 through $9 indicate the positional parameters when you call a function. In the example above find_dirs takes one positional parameter only, and would be called as find_dirs /etc. You can also use $@ to refer to all positional parameters. Functions also accept redirection. You can call a function with find_dirs $1 > /dev/null; we also could declare it as follows:



                find_dirs(){
                find "$1" -type d
                }


                Note from man bash: "Functions are executed in the context of the current shell; no new process is created to interpret them". That means that you also should be aware of functions having ability to alter your shell execution environment - change variables and terminal settings.






                share|improve this answer













                Here's an essential procedure for declaring a permanent function:




                1. Open ~/.bashrc file in a text editor. Doesn't matter which text editor, so long as you know how to use it and so long as you open the /home/<username>/.bashrc



                2. At the end of the ~/.bashrc declare your own function, for instance:



                  find_dirs(){
                  find "$1" -type d
                  }


                3. Save and close the file.



                The ~/.bashrc file is read every time you open interactive shell (that is new terminal tab, login via ssh, or open TTY1 or other virtual console). This will not be available in script files, because ~/.bashrc is not read for non-interactive shells. It is also not available if you run bash with --norc option.



                If you want the function to be available immediately in the currently open tab, use source ~/.bashrc command.





                Functions take arguments just like regular commands. For example, $1 through $9 indicate the positional parameters when you call a function. In the example above find_dirs takes one positional parameter only, and would be called as find_dirs /etc. You can also use $@ to refer to all positional parameters. Functions also accept redirection. You can call a function with find_dirs $1 > /dev/null; we also could declare it as follows:



                find_dirs(){
                find "$1" -type d
                }


                Note from man bash: "Functions are executed in the context of the current shell; no new process is created to interpret them". That means that you also should be aware of functions having ability to alter your shell execution environment - change variables and terminal settings.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 at 10:05









                Sergiy KolodyazhnyySergiy Kolodyazhnyy

                75.1k9155327




                75.1k9155327






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • 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%2faskubuntu.com%2fquestions%2f344557%2fwhere-can-i-put-a-user-defined-shell-function%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

                    How did Captain America manage to do this?

                    迪纳利

                    南乌拉尔铁路局