UNIX / Linux: Set your PATH Variable Using set or export Command

本文介绍如何在Linux和Unix-like系统中查看和修改PATH环境变量。教程涵盖使用不同shell(如bash、csh等)的方法,并提供示例说明如何永久设置路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How do I add a new path to $PATH variable under Linux and UNIX like operating system? What is my path, and how do I set or modify it using csh/tcsh or bash/ksh/sh shell?

The PATH is an environment variable. It is a colon delimited list of directories that your shell searches through when you enter a command. All executables are kept in different directories on the Linux and Unix like operating systems.
Tutorial details 
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time5m

Finding out your current path

To find out what your current path setting, type the following command at shell prompt. Open the Terminal and then enter:

 
echo "$PATH"
 

OR

 
printf "%s\n" "$PATH"
 

Sample outputs:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/sbin/modemZapp:/Users/vivek/gcutil-1.8.4

How do I modify my path?

To modify your path edit $PATH variable as per your shell. The syntax for setting path under UNIX / Linux dependent upon your login shell.

Bash, Sh, Ksh shell syntax to modify $PATH

If you are using bash, sh, or ksh, at the shell prompt, type:

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
export PATH=$PATH:/path/to/dir1
export PATH=$PATH:/path/to/dir1:/path/to/dir2
 

OR

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
PATH=$PATH:/path/to/dir1; export PATH
 

Please feel free to replace /path/to/dir1 with the directory you want the shell to search.

Tcsh or csh shell syntax to modify $PATH

If you are using tcsh or csh, shell enter:

 ## please note 'path' is case sensitivity and must be in lowercase ##
set path = ($path /path/to/dir1)
set path = ($path /path/to/dir1 /path/to/dir2)
 

OR

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
setenv PATH $PATH:/path/to/dir1
setenv PATH $PATH:/path/to/dir1:/path/to/dir2
 

Please feel free to replace /path/to/dir1 with the directory you want the shell to search.

Examples

In this example add /usr/local/bin to your path under BASH/ksh/sh shell, enter:

 
export PATH=$PATH:/usr/local/bin
 

OR

 
PATH=$PATH:/usr/local/bin; export PATH
 

To make these changes permanent, add the commands described above to the end of your ~/.profile file for sh and ksh shell, or ~/.bash_profile file for bash shell:

## BASH SHELL ##
echo 'export PATH=$PATH:/usr/local/bin'  >> ~/.bash_profile
 

KSH/sh shell user try:

## KSH / SH SHELL ##
echo 'export PATH=$PATH:/usr/local/bin'  >> ~/.profile
 

In this final example add /usr/local/bin/ and /scripts/admin/ to your path under csh / tcsh shell, enter:

 
set path = ($path /usr/local/bin /scripts/admin)
 

OR

 
setenv PATH $PATH:/usr/local/bin:/scripts/admin
 

To make these changes permanent, add the commands described above to the end of your ~/.cshrc file:

 
echo 'set path = ($path /usr/local/bin /scripts/admin)'  >> ~/.cshrc
 

OR

 
echo 'setenv PATH $PATH:/usr/local/bin:/scripts/admin'  >> ~/.cshrc
 

To verify new path settings, enter:
$ echo $PATH

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值