[리눅스] 환경변수 -1 (bash_profile, bashrc)

앙금빵·2021년 8월 8일
1

리눅스

목록 보기
10/13
post-thumbnail

Bash 환경 설정 ~/.bash_profile ~/.bashrc

~/.bash_profile

# HOME, bash 설정 파일

# .bash_profile

# Get the aliases and functions

----------------
# ~f = 파일의 존재 여부를 물음 -f ~/.bashrc 당신의 홈디렉토리에 .bashrc가 존재함?
# 참이면 ". ~/.bashrc" 명령어가 실행됨
# "." 은 파일을 읽어오라는 의미
# 홈 디렉토리에 .bashrc가 존재하면 ". ~/.bashrc"명령어 실행 -> "~/.bashrc" 파일을 읽어옴

if [ -f ~/.bashrc ]; then
        . ~/.bashrc

fi
----------------

# PATH 라는 변수에 설정을 하는 것
PATH=$PATH:$HOME/.local/bin:$HOME/bin

# PATH=$PATH
#1. PATH=$PATH -> PATH 라는 변수에 $PATH(기존 PATH 값) 을 넣고
#2. 그 다음에 $HOME/.local/bin 추가

# $HOME/.local/bin:$HOME/bin
#1. HOME 밑에 local/bin 을 추가
#2. 그 다음에 $HOME bin 을 추가  

----------------
# 환경변수로 export 쓰고 출력하기위해 나 echo를 사용
export PATH

~/.bashrc

 # .bashrc

# Source global definitions

# (해석) "/etc/bashrc" 가 존재하면 ". /etc/bashrc" 명령어 실행 -> "/etc/bashrc" 파일을 읽어옴
if [ -f /etc/bashrc ]; then
. /etc/bashrc
 fi

/etc/bashrc

  • 향후 시스템 관리까지 한다면 계속 들여다 볼 디렉토리
  • (line 41)PS1="[\u@\h \W]\\$ " PS1 = 프롬프트 상태를 바꿔줄 수 있는 것
  • (line 76) SHELL=/bin/bash "SHELL" 이라는 환경 변수에 /bin/bash 로 설정
  • 해당 파일은 되도록 건드리지 않는 것을 권장
# /etc/bashrc
  
# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
      fi
      ;;
    screen*)
      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
      fi
      ;;
    *)
      ;;
    esac
  fi
  # Turn on parallel history
  shopt -s histappend
  history -a
  # Turn on checkwinsize
  shopt -s checkwinsize
  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  # You might want to have e.g. tty in prompt (e.g. more virtual machines)
  # and console windows
  # If you want to do so, just add e.g.
  # if [ "$PS1" ]; then
  #   PS1="[\u@\h:\l \W]\\$ "
  # fi
  # to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell
  1 # /etc/bashrc
  2 
  3 # System wide functions and aliases
  4 # Environment stuff goes in /etc/profile
  5 
  6 # It's NOT a good idea to change this file unless you know what you
  7 # are doing. It's much better to create a custom.sh shell script in
  8 # /etc/profile.d/ to make custom changes to your environment, as this
  9 # will prevent the need for merging in future updates.
 10 
 11 # are we an interactive shell?
 12 if [ "$PS1" ]; then
 13   if [ -z "$PROMPT_COMMAND" ]; then
 14     case $TERM in
 15     xterm*|vte*)
 16       if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
 17           PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
 18       elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
 19           PROMPT_COMMAND="__vte_prompt_command"
 20       else
 22       fi
 23       ;;
 24     screen*)
 25       if [ -e /etc/sysconfig/bash-prompt-screen ]; then
 26           PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
 27       else
 29       fi
 30       ;;
 31     *)
 33       ;;
 34     esac
 35   fi
 36   # Turn on parallel history
 37   shopt -s histappend
 38   history -a
 39   # Turn on checkwinsize
 40   shopt -s checkwinsize
 41   [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
 42   # You might want to have e.g. tty in prompt (e.g. more virtual machines)
 43   # and console windows
 44   # If you want to do so, just add e.g.
 45   # if [ "$PS1" ]; then
 46   #   PS1="[\u@\h:\l \W]\\$ "
 47   # fi
 48   # to your custom modification shell script in /etc/profile.d/ directory
 49 fi
 50 
 51 if ! shopt -q login_shell ; then # We're not a login shell
 52     # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
 53     pathmunge () {
 54         case ":${PATH}:" in
 55             *:"$1":*)
 56                 ;;
 57             *)
 58                 if [ "$2" = "after" ] ; then
 59                     PATH=$PATH:$1
 60                 else
 61                     PATH=$1:$PATH
 62                 fi
 63         esac
 64     }
 65 
 66     # By default, we want umask to get set. This sets it for non-login shell.
 67     # Current threshold for system reserved uid/gids is 200
 68     # You could check uidgid reservation validity in
 69     # /usr/share/doc/setup-*/uidgid file
 70     if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
 71        umask 002
 72     else
 73        umask 022
 74     fi
 75 
 76     SHELL=/bin/bash
 77     # Only display echos from profile.d scripts if we are no login shell
 78     # and interactive - otherwise just process them to set envvars
 79     for i in /etc/profile.d/*.sh; do
 80         if [ -r "$i" ]; then
 81             if [ "$PS1" ]; then
 82                 . "$i"
 83             else
 84                 . "$i" >/dev/null
 85             fi
 86         fi
 87     done
 88 
 89     unset i
 90     unset -f pathmunge
 91 fi
 92 # vim:ts=4:sw=4

환경변수 적용 확인

# 저장 후 바로 적용이 안됨
[ec2-user@ip-172-31-15-222 ~]$ echo $AGE

[ec2-user@ip-172-31-15-222 ~]$ echo $NAME

----
# . ~/.bash_profile = 해당 명령어는 로그아웃 했다가 로그인 (결과적으로)

[ec2-user@ip-172-31-15-222 ~]$ . ~/.bash_profile
[ec2-user@ip-172-31-15-222 ~]$ echo $AGE
30
[ec2-user@ip-172-31-15-222 ~]$ echo $NAME
이종현
----
# bash 기준 윗 part는 부모 Shell 아랫 part는 자식 Shell
# 즉, 위에 세팅한 변수들은 지역 변수를 의미 Not 전역 변수
[ec2-user@ip-172-31-15-222 ~]$ bash
[ec2-user@ip-172-31-15-222 ~]$ echo $AGE

[ec2-user@ip-172-31-15-222 ~]$ echo $NAME

----
# export는 "지역 변수"를 -> "전역 변수"화 시켜주는 역할
# 다시 로그인해도 해당 명령어가 실행 되기 위한 목적으로 사용

[ec2-user@ip-172-31-15-222 ~]$ export AGE
[ec2-user@ip-172-31-15-222 ~]$ bash
[ec2-user@ip-172-31-15-222 ~]$ echo $AGE
30
[ec2-user@ip-172-31-15-222 ~]$ echo $NAME
----

.bashrc 에 환경변수 저장 (Linux)

	1 # .bashrc
  2 
  3 # Source global definitions
  4 if [ -f /etc/bashrc ]; then
  5         . /etc/bashrc
  6 fi
  7 
  8 # Uncomment the following line if you don't like systemctl's auto-paging feature:
  9 # export SYSTEMD_PAGER=
 10 
 11 # User specific aliases and functions
 12 
 13 alias h='history'
 14 alias c='clear'
 15 alias pt='ping -c3 8.8.8.8'

**※주의※ (아래와 같이) 스페이스바 들어가지 말 것!**
 13 alias h = 'history'
 14 alias c = 'clear'
 15 alias pt = 'ping -c3 8.8.8.8'

스크립트 파일 환경변수 저장


# 아무 파일 생성
---
vi john.c

  1 #include <stdio.h>
  2 int main(void)
  3 {
  4         puts("\n 안녕하세요 이종현입니다.\n");
  5         return 0;
  6 }
  7
---
# john만 입력하면 john.c 파일을 불러옴

# 방법 1.gcc (소스파일 명) -o (실행파일 명)
gcc john.c -o john

# 방법 2. gcc -o (실행파일 명) (소스파일 명)
gcc -o john john.c

# 저장 결과 확인
[ec2-user@ip-172-31-15-222 ~]$ ./john

 안녕하세요 이종현입니다.

# ./(이름) 역시 단축해보자

#1. echo를 통해 PATH 확인
[ec2-user@ip-172-31-15-222 ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin

#2. 기존 PATH 에 '.' 추가
[ec2-user@ip-172-31-15-222 ~]$ PATH=$PATH:.

#3. 추가되었는지 확인
[ec2-user@ip-172-31-15-222 ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin:.

#4. 이제 "./" 없이도 스크립트 파일을 단어로 실행이 가능하다.
[ec2-user@ip-172-31-15-222 ~]$ john

 안녕하세요 이종현입니다.

# Tip 서버 리부팅 후에도 해당 변수를 이용하고 싶다면 "bash_profile" 에 저장 후 . ~/.bash_profile
  • gcc 에 대한 설명

    (참조: https://codedosa.com/1048)

    C 나 C++로 된 Source를 컴파일 하기 위해서 리눅스에서는 주로 "gcc"라는 Open Source Compiler를 사용하여 컴파일을 한다.

    $ gcc -o (실행파일 명) (소스파일 명)

    자세한 내용 링크 참조

profile
Cloud 관련 개인 공부 지식들을 기록하는 공간입니다.

0개의 댓글