Saturday, May 4, 2024
19
rated 0 times [  19] [ 0]  / answers: 1 / hits: 20526  / 3 Years ago, sat, september 18, 2021, 10:47:15

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?


More From » command-line

 Answers
6

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).


[#29482] Monday, September 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ibuteking

Total Points: 35
Total Questions: 128
Total Answers: 138

Location: Indonesia
Member since Thu, Oct 1, 2020
4 Years ago
ibuteking questions
Sat, Sep 10, 22, 08:44, 2 Years ago
Fri, May 12, 23, 10:10, 1 Year ago
Tue, May 23, 23, 13:38, 1 Year ago
;