Last Updated: December 21, 2023
·
372
· Lex S

Write your own bash commands with functions

Here are some functions you can add to your ".bash_profile" that replicate built-in commands.
function echo() { /bin/cat << EOC $@ EOC }

function whoami() { /usr/bin/id -u -n }

function pwd() { echo $PWD }

Writing your own bash functions can be very useful to automate local development tasks.

Mailer To Go is an email provider on Heroku that is by developers for developers.
We've spend too much time dealing with other mailing services, and decided to create our own.

1 Response
Add your response

Let me provide a brief explanation of the functions you've shared:

echo(): This function overrides the built-in echo command. It uses /bin/cat to display the provided arguments as standard input using a "here document" (<< EOC). This function essentially replicates the functionality of the standard echo command but in a more complex way.

whoami(): This function replicates the whoami command by using /usr/bin/id -u -n to display the current user's username.

pwd(): This function replicates the pwd (print working directory) command by echoing the value of the PWD environment variable.

These functions can be added to your .bash_profile file to customize your shell environment. However, be cautious when overriding built-in commands like echo, as it can lead to confusion and unexpected behavior when working with scripts or other tools that rely on these commands.

6 months ago ·