亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Linux 環(huán)境變量配置" >Linux 環(huán)境變量配置
Linux 讀取環(huán)境變量" >Linux 讀取環(huán)境變量
Linux環(huán)境變量配置方法一:export PATH" >Linux環(huán)境變量配置方法一:export PATH
Linux環(huán)境變量配置方法二:vim ~/.bashrc" >Linux環(huán)境變量配置方法二:vim ~/.bashrc
Linux環(huán)境變量配置方法三:vim ~/.bash_profile" >Linux環(huán)境變量配置方法三:vim ~/.bash_profile
Linux環(huán)境變量配置方法四:vim /etc/bashrc" >Linux環(huán)境變量配置方法四:vim /etc/bashrc
Linux環(huán)境變量配置方法五:vim /etc/profile" >Linux環(huán)境變量配置方法五:vim /etc/profile
Linux環(huán)境變量配置方法六:vim /etc/environment" >Linux環(huán)境變量配置方法六:vim /etc/environment
Linux環(huán)境變量加載原理解析" >Linux環(huán)境變量加載原理解析
環(huán)境變量的分類" >環(huán)境變量的分類
測試Linux環(huán)境變量加載順序的方法" >測試Linux環(huán)境變量加載順序的方法
Linux環(huán)境變量文件加載詳解" >Linux環(huán)境變量文件加載詳解
一些小技巧" >一些小技巧
Home System Tutorial LINUX 6 ways to configure Linux environment variables, recommended to collect!

6 ways to configure Linux environment variables, recommended to collect!

Feb 14, 2024 pm 05:00 PM
linux linux tutorial linux system linux command shell script Getting started with linux linux learning

Linux 環(huán)境變量配置

在自定義安裝軟件時,經(jīng)常需要配置環(huán)境變量。下面列舉了各種配置環(huán)境變量的方法。

下面所有例子的環(huán)境說明如下:

  • 系統(tǒng):Ubuntu 14.0
  • 用戶名:uusama
  • 需要配置 MySQL 環(huán)境變量路徑:/home/uusama/mysql/binLinux 環(huán)境變量配置的 6 種方法,建議收藏!

Linux 讀取環(huán)境變量

讀取環(huán)境變量的方法:

  • export 命令顯示當(dāng)前系統(tǒng)定義的所有環(huán)境變量
  • echo $PATH 命令輸出當(dāng)前的 PATH 環(huán)境變量的值

這兩個命令執(zhí)行的效果如下

uusama@ubuntu:~export
declare?-x?HOME="/home/uusama"
declare?-x?LANG="en_US.UTF-8"
declare?-x?LANGUAGE="en_US:"
declare?-x?LESSCLOSE="/usr/bin/lesspipe?%s?%s"
declare?-x?LESSOPEN="|?/usr/bin/lesspipe?%s"
declare?-x?LOGNAME="uusama"
declare?-x?MAIL="/var/mail/uusama"
declare?-x?PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare?-x?SSH_TTY="/dev/pts/0"
declare?-x?TERM="xterm"
declare?-x?USER="uusama"

uusama@ubuntu:~?echo?$PATH
/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

其中PATH變量定義了運(yùn)行命令的查找路徑,以冒號:分割不同的路徑,使用export定義的時候可加雙引號也可不加。

Linux環(huán)境變量配置方法一:export PATH

使用export命令直接修改PATH的值,配置MySQL進(jìn)入環(huán)境變量的方法:

export?PATH=/home/uusama/mysql/bin:PATH

#?或者把PATH放在前面
export?PATH=PATH:/home/uusama/mysql/bin

注意事項:

  • 生效時間:立即生效
  • 生效期限:當(dāng)前終端有效,窗口關(guān)閉后無效
  • 生效范圍:僅對當(dāng)前用戶有效
  • 配置的環(huán)境變量中不要忘了加上原來的配置,即$PATH部分,避免覆蓋原來配置

Linux環(huán)境變量配置方法二:vim ~/.bashrc

通過修改用戶目錄下的~/.bashrc文件進(jìn)行配置:

vim?~/.bashrc

#?在最后一行加上
export?PATH=$PATH:/home/uusama/mysql/bin

注意事項:

  • 生效時間:使用相同的用戶打開新的終端時生效,或者手動source ~/.bashrc生效
  • 生效期限:永久有效
  • 生效范圍:僅對當(dāng)前用戶有效
  • 如果有后續(xù)的環(huán)境變量加載文件覆蓋了PATH定義,則可能不生效

Linux環(huán)境變量配置方法三:vim ~/.bash_profile

和修改~/.bashrc文件類似,也是要在文件最后加上新的路徑即可:

vim?~/.bash_profile

#?在最后一行加上
export?PATH=$PATH:/home/uusama/mysql/bin

注意事項:

  • 生效時間:使用相同的用戶打開新的終端時生效,或者手動source ~/.bash_profile生效
  • 生效期限:永久有效
  • 生效范圍:僅對當(dāng)前用戶有效
  • 如果沒有~/.bash_profile文件,則可以編輯~/.profile文件或者新建一個

Linux環(huán)境變量配置方法四:vim /etc/bashrc

該方法是修改系統(tǒng)配置,需要管理員權(quán)限(如root)或者對該文件的寫入權(quán)限:

#?如果/etc/bashrc文件不可編輯,需要修改為可編輯
chmod?-v?u+w?/etc/bashrc

vim?/etc/bashrc

#?在最后一行加上
export?PATH=$PATH:/home/uusama/mysql/bin

注意事項:

  • 生效時間:新開終端生效,或者手動source /etc/bashrc生效
  • 生效期限:永久有效
  • 生效范圍:對所有用戶有效

Linux環(huán)境變量配置方法五:vim /etc/profile

該方法修改系統(tǒng)配置,需要管理員權(quán)限或者對該文件的寫入權(quán)限,和vim /etc/bashrc類似:

#?如果/etc/profile文件不可編輯,需要修改為可編輯
chmod?-v?u+w?/etc/profile

vim?/etc/profile

#?在最后一行加上
export?PATH=$PATH:/home/uusama/mysql/bin

注意事項:

  • 生效時間:新開終端生效,或者手動source /etc/profile生效
  • 生效期限:永久有效
  • 生效范圍:對所有用戶有效

Linux環(huán)境變量配置方法六:vim /etc/environment

該方法是修改系統(tǒng)環(huán)境配置文件,需要管理員權(quán)限或者對該文件的寫入權(quán)限:

#?如果/etc/bashrc文件不可編輯,需要修改為可編輯
chmod?-v?u+w?/etc/environment

vim?/etc/profile

#?在最后一行加上
export?PATH=$PATH:/home/uusama/mysql/bin

注意事項:

  • 生效時間:新開終端生效,或者手動source /etc/environment生效
  • 生效期限:永久有效
  • 生效范圍:對所有用戶有效

Linux環(huán)境變量加載原理解析

上面列出了環(huán)境變量的各種配置方法,那么Linux是如何加載這些配置的呢?是以什么樣的順序加載的呢?

特定的加載順序會導(dǎo)致相同名稱的環(huán)境變量定義被覆蓋或者不生效。

環(huán)境變量的分類

環(huán)境變量可以簡單的分成用戶自定義的環(huán)境變量以及系統(tǒng)級別的環(huán)境變量。

  • 用戶級別環(huán)境變量定義文件:~/.bashrc、~/.profile(部分系統(tǒng)為:~/.bash_profile)
  • 系統(tǒng)級別環(huán)境變量定義文件:/etc/bashrc、/etc/profile(部分系統(tǒng)為:/etc/bash_profile)、/etc/environment

另外在用戶環(huán)境變量中,系統(tǒng)會首先讀取~/.bash_profile(或者~/.profile)文件,如果沒有該文件則讀取~/.bash_login,根據(jù)這些文件中內(nèi)容再去讀取~/.bashrc。

測試Linux環(huán)境變量加載順序的方法

為了測試各個不同文件的環(huán)境變量加載順序,我們在每個環(huán)境變量定義文件中的第一行都定義相同的環(huán)境變量UU_ORDER,該變量的值為本身的值連接上當(dāng)前文件名稱。

需要修改的文件如下:

  • /etc/environment
  • /etc/profile
  • /etc/profile.d/test.sh,新建文件,沒有文件夾可略過
  • /etc/bashrc,或者/etc/bash.bashrc
  • ~/.bash_profile,或者~/.profile
  • ~/.bashrc

在每個文件中的第一行都加上下面這句代碼,并相應(yīng)的把冒號后的內(nèi)容修改為當(dāng)前文件的絕對文件名。

export?UU_ORDER="$UU_ORDER:~/.bash_profile"

修改完之后保存,新開一個窗口,然后echo $UU_ORDER觀察變量的值:

uusama@ubuntu:~echoUU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc

可以推測出Linux加載環(huán)境變量的順序如下:

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /etc/profile.d/test.sh
  5. ~/.profile
  6. ~/.bashrc

Linux環(huán)境變量文件加載詳解

由上面的測試可容易得出Linux加載環(huán)境變量的順序如下,:

系統(tǒng)環(huán)境變量 -> 用戶自定義環(huán)境變量 /etc/environment -> /etc/profile -> ~/.profile

打開/etc/profile文件你會發(fā)現(xiàn),該文件的代碼中會加載/etc/bash.bashrc文件,然后檢查/etc/profile.d/目錄下的.sh文件并加載。

#?/etc/profile:?system-wide?.profile?file?for?the?Bourne?shell?(sh(1))
#?and?Bourne?compatible?shells?(bash(1),?ksh(1),?ash(1),?...).

if?[?"PS1"?];?then
??if?[?"BASH"?]?&&?[?"BASH"?!=?"/bin/sh"?];?then
????#?The?file?bash.bashrc?already?sets?the?default?PS1.
????#?PS1='\h:\w\$?'
????if?[?-f?/etc/bash.bashrc?];?then
??????.?/etc/bash.bashrc
????fi
??else
????if?[?"`id?-u`"?-eq?0?];?then
??????PS1='#?'
????else
??????PS1='?'
????fi
??fi
fi

if?[?-d?/etc/profile.d?];?then
??for?i?in?/etc/profile.d/*.sh;?do
????if?[?-r?i?];?then
??????.i
????fi
??done
??unset?i
fi

其次再打開~/.profile文件,會發(fā)現(xiàn)該文件中加載了~/.bashrc文件。

#?if?running?bash
if?[?-n?"BASH_VERSION"?];?then
????#?include?.bashrc?if?it?exists
????if?[?-f?"HOME/.bashrc"?];?then
????.?"HOME/.bashrc"
????fi
fi

#?set?PATH?so?it?includes?user's?private?bin?directories
PATH="HOME/bin:HOME/.local/bin:PATH"

從~/.profile文件中代碼不難發(fā)現(xiàn),/.profile文件只在用戶登錄的時候讀取一次,而/.bashrc會在每次運(yùn)行Shell腳本的時候讀取一次。

一些小技巧

可以自定義一個環(huán)境變量文件,比如在某個項目下定義uusama.profile,在這個文件中使用export定義一系列變量,然后在~/.profile文件后面加上:sourc uusama.profile,這樣你每次登陸都可以在Shell腳本中使用自己定義的一系列變量。

也可以使用alias命令定義一些命令的別名,比如alias rm=”rm -i”(雙引號必須),并把這個代碼加入到~/.profile中,這樣你每次使用rm命令的時候,都相當(dāng)于使用rm -i命令,非常方便。

The above is the detailed content of 6 ways to configure Linux environment variables, recommended to collect!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to install Linux alongside Windows (dual boot)? How to install Linux alongside Windows (dual boot)? Jun 18, 2025 am 12:19 AM

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

How to enable the EPEL (Extra Packages for Enterprise Linux) repository? How to enable the EPEL (Extra Packages for Enterprise Linux) repository? Jun 17, 2025 am 09:15 AM

The key to enabling EPEL repository is to select the correct installation method according to the system version. First, confirm the system type and version, and use the command cat/etc/os-release to obtain information; second, enable EPEL through dnfinstallepel-release on CentOS/RockyLinux, and the 8 and 9 version commands are the same; third, you need to manually download the corresponding version of the .repo file and install it on RHEL; fourth, you can re-import the GPG key when encountering problems. Note that the old version may not be supported, and you can also consider enabling epel-next to obtain the test package. After completing the above steps, use dnfrepolist to verify that the EPEL repository is successfully added.

How to choose a Linux distro for a beginner? How to choose a Linux distro for a beginner? Jun 19, 2025 am 12:09 AM

Newbie users should first clarify their usage requirements when choosing a Linux distribution. 1. Choose Ubuntu or LinuxMint for daily use; programming and development are suitable for Manjaro or Fedora; use Lubuntu and other lightweight systems for old devices; recommend CentOSStream or Debian to learn the underlying principles. 2. Stability is preferred for UbuntuLTS or Debian; you can choose Arch or Manjaro to pursue new features. 3. In terms of community support, Ubuntu and LinuxMint are rich in resources, and Arch documents are technically oriented. 4. In terms of installation difficulty, Ubuntu and LinuxMint are relatively simple, and Arch is suitable for those with basic needs. It is recommended to try it first and then decide.

How to add a new disk to Linux How to add a new disk to Linux Jun 27, 2025 am 12:15 AM

The steps to add a new hard disk to the Linux system are as follows: 1. Confirm that the hard disk is recognized and use lsblk or fdisk-l to check; 2. Use fdisk or parted partitions, such as fdisk/dev/sdb and create and save; 3. Format the partition to a file system, such as mkfs.ext4/dev/sdb1; 4. Use the mount command for temporary mounts, such as mount/dev/sdb1/mnt/data; 5. Modify /etc/fstab to achieve automatic mount on the computer, and test the mount first to ensure correctness. Be sure to confirm data security before operation to avoid hardware connection problems.

Where are system logs located in Linux? Where are system logs located in Linux? Jun 24, 2025 am 12:15 AM

Logs in Linux systems are usually stored in the /var/log directory, which contains a variety of key log files, such as syslog or messages (record system logs), auth.log (record authentication events), kern.log (record kernel messages), dpkg.log or yum.log (record package operations), boot.log (record startup information); log content can be viewed through cat, tail-f or journalctl commands; application logs are often located in subdirectories under /var/log, such as Apache's apache2 or httpd directory, MySQL log files, etc.; at the same time, it is necessary to note that log permissions usually require s

Fixed the failure to upload files in Windows Google Chrome Fixed the failure to upload files in Windows Google Chrome Jul 08, 2025 pm 02:33 PM

Have problems uploading files in Google Chrome? This may be annoying, right? Whether you are attaching documents to emails, sharing images on social media, or submitting important files for work or school, a smooth file upload process is crucial. So, it can be frustrating if your file uploads continue to fail in Chrome on Windows PC. If you're not ready to give up your favorite browser, here are some tips for fixes that can't upload files on Windows Google Chrome 1. Start with Universal Repair Before we learn about any advanced troubleshooting tips, it's best to try some of the basic solutions mentioned below. Troubleshooting Internet connection issues: Internet connection

What is the sudo command and when should I use it? What is the sudo command and when should I use it? Jul 02, 2025 am 12:20 AM

sudo stands for "substituteuserdo" or "superuserdo", allowing users to run commands with permissions of other users (usually root). Its core uses include: 1. Perform system-level operations such as installing software or editing system files; 2. Accessing protected directories or logs; 3. Manage services such as restarting nginx; 4. Modify global settings such as /etc/hosts. When using it, the system will check the /etc/sudoers configuration and verify the user password, provide temporary permissions instead of continuously logging in as root, ensuring security. Best practices include: only when necessary, avoid blindly executing network commands, editing sudoers files with visudo, and considering continuous operations.

How to manage groups on Linux How to manage groups on Linux Jul 06, 2025 am 12:02 AM

To manage Linux user groups, you need to master the operation of viewing, creating, deleting, modifying, and user attribute adjustment. To view user group information, you can use cat/etc/group or getentgroup, use groups [username] or id [username] to view the group to which the user belongs; use groupadd to create a group, and use groupdel to specify the GID; use groupdel to delete empty groups; use usermod-aG to add users to the group, and use usermod-g to modify the main group; use usermod-g to remove users from the group by editing /etc/group or using the vigr command; use groupmod-n (change name) or groupmod-g (change GID) to modify group properties, and remember to update the permissions of relevant files.

See all articles