天天看点

设置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,添加行: 

使生效 

source .bashrc 

三、所有用户的全局设置 

$ vim /etc/profile 

在里面加入: 

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 命令 

完成。 

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

【以上转自】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 

  else 

    if [ "`id -u`" -eq 0 ]; then 

      ps1='# ' 

    else 

      ps1='$ ' 

  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:~$ 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" 

# set path so it includes user's private bin if it exists 

if [ -d "$home/bin" ] ; then 

    path="$home/bin:$path" 

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) 

# 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 

color_prompt= 

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\$ ' 

unset color_prompt force_color_prompt 

# if this is an xterm set the title to user@host:dir 

xterm*|rxvt*) 

    ps1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$ps1" 

    ;; 

*) 

# 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' 

# 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 

# 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 

echo $path 

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: