Add custom text to terminal when you launch it

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to add a custom text or function at the top of your terminal, everytime you launch terminal. For example, we are going to add a custom command that will show us the logged in users in our system whenever terminal is launched. The function will get executed on any terminal session.

Step 1 - Open ~./bashrc file

Bashrc is the file that contains the configuration for the terminal session, everytime you launch your terminal the bashrc file gets executed, since we want a custom text to print and a command to execute whenever we launch terminal, all we have to do is to add this custom command to bashrc file.

Make sure you have sudo access to be able to edit bashrc file, run the command below to edit bashrc using any text editor you like.

sudo nano ~/.bashrc

Step 2 - Add custom text to bashrc

Once you open bashrc, copy the code below and paste it to bashrc file. To make it easy to read and change the file later if you want to, add the code at the end of the file.

echo "Hi, ${USER}, the logged in users are:"
w

Next, save the changes, by pressing CTRL + X then confirm by pressing Y. To be able to make those changes permanent, you need to source the bashrc file, to do this simply run

source ~/.bashrc

Once sourced, reboot your system to verify that the code that you added is working. If it doesn't work, make sure the account that you are using to edit bashrc file has correct permissions.

Step 3 - Launch terminal

Now, simply launch terminal, and the custom function that you added in the bashrc file should get executed and the output should show at the top of the terminal, as shown in the image below.

Conclusion

In this tutorial, you learned how to add a custom function that will execute the w command to display the current logged in users. This was just an example of how you can add a custom function that gets executed whenever the terminal is launched.