设置Ubuntu环境变量 PATH 的方法 及配置备份

在Ubuntu中有如下几个文件可以设置环境变量


/etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.

/etc/environment:在登录时操作系统使用的第二个文件,系统在读取你自己的profile前,设置环境文件的环境变量

~/.profile:在登录时用到的第三个文件是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.

/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.

~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取.

通常设置环境变量有三种方法:
一、临时设置
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH

二、当前用户的全局设置
打开~/.bashrc,添加行:
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH

使生效

source .bashrc
三、所有用户的全局设置
$ vim /etc/profile
在里面加入:
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH
使生效
source profile

测试 echo $PATH
有两种设置环境变量 PATH 的方法。第一种适用于为单一用户设置 PATH,第二种是为全局设置 PATH。

1.在用户主目录下有一个 .bashrc 文件,可以在此文件中加入 PATH 的设置如下:
用gedit /root/.bashrc打开
export PATH=”$PATH:/your path1/:/your path2/…..”
注意:每一个 path 之间要用 “:“ 分隔。

2.在/etc/profile中增加。
PATH="$PATH:/home/zhengb66/bin"
export PATH
如果要使得即时生效,使用 source 命令
source .bashrc
完成。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

【以上转自】http://blog.mcuol.com/User/For_ATIW/Article/11709_1.htm,另以下所介绍的方法只在一个终端中生效,另外打开新的终端发现PATH还是原来的PATH。要永久改变PATH 参见本文末的介绍~

PS1:直接修改文件内容的方法没有尝试过——针对方法1,因为在虚拟机下安装的,没有root权限修改/root下的文件;
针对方法2,在Ubuntu中找到的/etc/profile写得很诡异,看不懂,没找到类似“PATH=***”的内容。

PS2:自己琢磨成功——在终端中用命令的形式修改:
~$ echo $PATH      # 显示当前环境变量
/usr/local/sbin:/usr/local/bin    # 显示举例,多个路径中间用“:”隔开
~$ PATH="$PATH:/home/workdir"    # 添加“/home/workdir”目录
/usr/local/sbin:/usr/local/bin:/home/workdir   # 显示举例,多个路径中间仍用“:”隔开

则添加成功!(最后当然可以重启或者输出使其即时生效:)
~$ export PATH
~$ cd ~
~$ source .bashrc     #该命令需在主目录下才能运行,否则提示找不到.bashrc命令
结束!

PS3:写命令的时候严格按此形式,在“=”前后千万不要有空格,否则提示“bash: PATH: command not found”错误。笔者曾吃过这样的狠亏,主要严格遵照上边的方法就可以解决问题了。

【永久性改变PATH的方法】
参考http://hi.baidu.com/youjim/blog/item/da912cd98475b3ef39012fad.html

打开~/.bashrc文件最末添加命令:
PATH=$PATH:/home/workdir
注意与上文中各式不同,没有双引号,重启解决。
为了您的安全,请只打开来源可靠的网址

打开网站    取消
来自: http://hi.baidu.com/%D6%AA%D3%EA%CF%FE%C7%E7/blog/item/11962a9690944815d31b7069.html

roo@ubuntu:~$ cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

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

if [ "$PS1" ]; then
  if [ "$BASH" ]; then
    PS1='\u@\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

umask 022
roo@ubuntu:~$

roo@ubuntu:~$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:"
roo@ubuntu:~$

roo@ubuntu:~$ cd ~
roo@ubuntu:~$ cat /.profile
cat: /.profile: 没有那个文件或目录
roo@ubuntu:~$ cat .profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# 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 if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
roo@ubuntu:~$

roo@ubuntu:~$ cat .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
    else
color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi
roo@ubuntu:~$

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:

猜你喜欢

转载自jadeluo.iteye.com/blog/940113