Terminal is a tool that we Linux users use almost on a daily basis. whether for basic tasks like copying and moving files, or advanced commands such as network configuration or running a web server. The terminal is an incredibly versatile tool that significantly enhances our efficiency.
Other than doing regular tasks, your terminal can actually be customized to run specific commands each time you open it. This feature is actually pretty handy because you can do a lot of useful things every time you launch the terminal.
In this article, you will learn how to run commands every time the terminal opens. By the end of this article, you should be able to use custom commands to run on the terminal startup.
Check Your Current Active Shell
Before you can configure the shell configuration, you need to know which shell you are currently using. You can do this by using the echo
command:
echo $SHELL
Executing this command will tell you about your currently active shell. Once you have identified the active shell, you can proceed to the next step.
Run Commands on Terminal Startup With Bash
Now that you already know your default shell, if you are using bash, you’ll need to modify the .bashrc
file located in your home directory.
The .bashrc
is the default configuration file for the Bash shell. This file defines many things, including environment variables, aliases, functions, and terminal behavior.
So basically we will be editing the .bashrc
file in order to run commands on terminal startup. You can use nano or vim text editor to modify the .bashrc
file.
nano ~/.bashrc
Within the .bashrc
file, navigate to the very bottom. There, you can insert any command you wish to run each time the terminal is launched.
For instance, to execute the screenfetch
command on startup, add it to the .bashrc
file.
Now save the .bashrc
file, and you can close the terminal and reopen it, and voila! The screenfetch
command should be automatically executed.
Example Result
This is the example result of running screenfetch
during the terminal initialization. It displays system information with a nice appearance, and I also added colorful styling by combining it with the figlet command.
Run Commands on Terminal Startup With Zsh
If you are using Zsh, the overall process should be quite similar to what we did with Bash. So Essentially, you’ll need to edit the .zshrc
file.
Use either nano or vim to edit the .zshrc
file. From your terminal’s home directory, execute the following command:
nano .zshrc
Scroll to the end of the .zshrc
file.
Here, you can input any command you’d like to run when the terminal starts. For example, you can execute the “screenfetch” command or perhaps use the echo command to display a greeting.
Once you’ve added the desired commands to the .zshrc
file, save it and restart the terminal to see the effect.
Example Result
For example, here I set my terminal to show the date in ASCII style with with colorful formatting achieved by combining the date
, figlet
, and lolcat
commands.
Really cool, isn’t it? So basically, you can run anything in the initial terminal startup. And in the next sections, I will show you things that you can do in the terminal startup.
Things You Can Do on the Terminal Startup
Most of the time, the Linux terminal runs bash or zsh as the default shell. These shells provide configuration files that users can customize according to their needs.
Essentially, any command placed in the shell configuration file will be executed during terminal initialization. In other words, you can include various commands or scripts in the .bashrc
or .zshrc
file.
Run a Script
One of the common things you can do in the terminal startup is run a script. Whether it’s just a simple bash script or an advanced one, you can easily do that. Simply by putting the full path to the script file in the .bashrc
or .zshrc
, the script will be automatically executed every time the terminal opens.
For example, if you want to run a script called myscript.sh
located in the Documents folder, you can write the full path to the myscript.sh
similar to the below:
/home/randy/Documents/myscript.sh
Or you can write it shorter as follows:
~/Documents/myscript.sh
Save the configuration file, then close your terminal and reopen it again. Now the script should be automatically executed.
Show Screenfetch
The other thing you can do on terminal startup is show Screenfetch. Screenfetch is a tool to display system information in an aesthetic way. You can display various things such as CPU, GPU, memory usage, available disk, and many more.
To automatically execute screenfetch whenever the terminal is launched, you can conveniently insert the screenfetch
command at the end of either the .bashrc
or .zshrc
file.
show ASCII Art
You can also improve your terminal’s visual appeal by showcasing impressive ASCII text art using figlet
command.
ASCII art is a great way to make your terminal look super cool. You can make any text fancy by turning it into ASCII art. One of the tools that can transform simple text into ASCII art is Figlet.
Before you can use Figlet, you will need to install it first. If you are using an Ubuntu-based distribution simply run the following command to install Figlet:
sudo apt install figlet
Once Figlet is successfully installed on your system, you can generate ASCII text art using the figlet
command. For instance:
figlet "Linux is cool"
The output will be like this:
So by utilizing figlet, you can show fancy ASCII art every time you open the terminal. This will definitely take your terminal customization to another level.
Similar to a regular command, to ensure automatic execution of the figlet
command on the terminal startup, simply place the figlet
command at the end of the .bashrc or .zshrc file.
Display Current Time in ASCII Style
Rather than presenting mere plain text, you can use figlet to display the current time in ASCII style.
Using the following command, you can effectively showcase the current time in ASCII:
date +'%I:%M %p' | figlet
Add this line of command and put it at the end of .bashrc
or .zshrc
, you can automatically show the current time in ASCII style every time you open the terminal.
Troubleshooting
Sometimes things just don’t work as we expected, There are common problems that may occur when you try adding a command to the .bashrc
or .zshrc
file. Let’s find out and see if we can fix them.
Console Output During Zsh Initialization Detected
If you’ve encountered this warning while launching the terminal, it’s likely due to your use of Powerlevel10k.
[WARNING]: Console output during zsh initialization detected.
This message arises when Powerlevel10k with an instant prompt is in use, and console output occurs during zsh initialization, potentially indicating issues.
To resolve this, you can dismiss the warning message by editing the .p10k.zsh
file located in your home directory.
nano ~/.p10k.zsh
Find the line containing “typeset -g POWERLEVEL9K_INSTANT_PROMPT=”, by default, the value is set to verbose, which is why the warning message comes up. So you need to change the value to quiet in order to dismiss the warning message.
The correct configuration should be like this:
typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet
After making this adjustment, save the .p10k.zsh
file and try to restart the terminal. If configured correctly, the warning message should no longer appear.
No Such File or Directory
This error is mostly caused by the wrong path or a typo in your script path. To resolve this, you must accurately specify the correct path to your script file.
To identify the precise full path to your script, you can employ the readlink
command:
readlink -s yourscript
This command will show you the complete path to your script. You can then use this path and include it in the .bashrc
or .zshrc
file. Now your script should be automatically executed without any problems.
Conclusion
And that’s how you can run commands on the terminal startup. By utilizing this, you can creatively do many cool things each time you open the terminal.
So go ahead, experiment with different commands, personalize your terminal environment, and make your daily terminal interactions not only productive but also uniquely enjoyable.