Some time ago, the Fedora Magazine has published an article introducing ZSH — an alternative shell to Fedora’s default, bash. This time, we’re going to look into customizing it to use it in a more effective way. All of the concepts shown in this article also work in other shells such as bash.
Aliases are shortcuts for commands. This is useful for creating short commands for actions that are performed often, but require a long command that would take too much time to type. The syntax is:
$ alias yourAlias='complex command with arguments'
They don’t always need to be used for shortening long commands. Important is that you use them for tasks that you do often. An example could be:
$ alias dnfUpgrade='dnf -y upgrade'
That way, to do a system upgrade, I just type dnfUpgrade instead of the whole dnf command.
The problem of setting aliases right in the console is that once the terminal session is closed, the alias would be lost. To set them permanently, resource files are used.
Resource files (or rc files) are configuration files that are loaded per user in the beginning of a session or a process (when a new terminal window is opened, or a new program like vim is started). In the case of ZSH, the resource file is .zshrc, and for bash it’s .bashrc.
To make the aliases permanent, you can either put them in your resource. You can edit your resource file with a text editor of your choice. This example uses vim:
$ vim $HOME/.zshrc
Or for bash:
$ vim $HOME/.bashrc
Note that the location of the resource file is specified relatively to a home directory — and that’s where ZSH (or bash) are going to look for the file by default for each user.
Other option is to put your configuration in any other file, and then source it:
$ source /path/to/your/rc/file
Again, sourcing it right in your session will only apply it to the session, so to make it permanent, add the source command to your resource file. The advantage of having your source file in a different location is that you can source it any time. Or anywhere which is especially useful in shared environments.
Environment variables are values assigned to a specific name which can be then called in scripts and commands. They start with the $ dollar sign. One of the most common is $HOME that references the home directory.
As the name suggests, environment variables are a part of your environment. Set a variable using the following syntax:
And to make it an environment variable, export it with the following command:
$ export http_proxy
To see all the environment variables that are currently set, use the env command:
The command outputs all the variables available in your session. To demonstrate how to use them in a command, try running the following echo commands:
$ echo $PWD
$ echo $USER
What happens here is variable expansion — the value stored in the variable is used in your command.
Another useful variable is $PATH, that defines directories that your shell uses to look for binaries.
The $PATH variable
There are many directories, or folders (the way they are called in graphical environments) that are important to the OS. Some directories are set to hold binaries you can use directly in your shell. And these directories are defined in the $PATH variable.
$ echo $PATH
This will help you when you want to have your own binaries (or scripts) accessible in the shell.