Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to manage the environment of Shell

Shulou Source: shulou.com Published: 2022-06-01 13:13:56 09月10日 Update

This article focuses on "how to manage the environment of Shell". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to manage the environment of Shell.

Alias

An alias is a shortcut to a command. It is useful to create shortcuts for long commands that need to be executed frequently but take a long time to enter. The syntax is:

$alias yourAlias='complex command with arguments'

They are not always used to shorten long commands. The important thing is that you use them for tasks you often perform. Possible examples:

$alias dnfUpgrade='dnf-y upgrade'

In this way, in order to upgrade the system, I just need to enter dnfUpgrade instead of entering the full dnf command.

The problem with setting an alias in a terminal is that once the terminal session is closed, the alias is lost. To set them, use a resource file.

Resource file

Resource files (that is, rc files) are configuration files that are loaded at the beginning of a session or process (when each user opens a new terminal window or starts a new program such as vim). For ZSH, the resource file is .zshrc, and for bash, it is .bashrc.

To make aliases * aliases, you can put them in the resource file. You can use the text editor of your choice to edit the resource file. Vim is used here:

$vim $HOME/.zshrc

Or for bash:

$vim $HOME/.bashrc

Note that the location of the resource file is specified relative to the home directory. This is the location where ZSH (or bash) looks for the file by default for each user.

Another way is to put your configuration in any other file and read it:

$source / path/to/your/rc/file

Similarly, reading it directly in a session will only apply it to the session, so to make it * *, add the source command to the resource file. The advantage of putting a file in a different location is that you can read it at any time. This is useful in a shared environment.

Environment variable

Environment variables are values that are assigned a specific name, and you can call them in scripts and commands. They start with the dollar sign ($). The most common of these is $HOME, which points to the home directory.

As the name implies, environmental variables are part of your environment. Set the variable using the following syntax:

$http_proxy= "http://your.proxy"

To make it an environment variable, export it using the following command:

$export $http_proxy

To view all environment variables that are currently set, use the env command:

$env

This command outputs all variables available in the session. To demonstrate how to use them in commands, try running the following echo command:

$echo $PWD/home/fedora$ echo $USERfedora

Variable expansion occurs here, where the values stored in the variables are used in the command.

Another useful variable is $PATH, which defines the directory where shell looks for binaries.

$PATH variable

There are many directories or folders that are important to the operating system (how they are called in a graphical environment). Some directories are set to hold binaries that can be used directly in shell. These directories are defined in the $PATH variable.

$echo $PATH/usr/lib64/qt-3.3/bin:/usr/share/Modules/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/usr/libexec/sdcc:/usr/libexec/sdcc:/usr/bin:/bin:/sbin:/usr/sbin:/opt/FortiClient

This is helpful when you want to access your binaries (or scripts) in shell.

At this point, I believe you have a deeper understanding of "how to manage the environment of Shell". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File variable environment command resource alias directory administration binary location mode useful terminal input important content user system script syntax Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn MySQL MariaDB Shulou Technology