newhaneul

[ROS2 Humble] 터미널과 bashrc 설정 본문

1. Programming/ROS

[ROS2 Humble] 터미널과 bashrc 설정

뉴하늘 2026. 1. 1. 18:26
728x90

본 포스팅은 '민형기 저자의 'ROS2 Humble 혼자 공부하는 로봇SW 직접 만들고 코딩하자'를 읽고 공부한 내용을 정리하기 위한 포스팅입니다.

1. bashrc

 

1.1. Shell 쉘

 Shell은 운영체제 일부로서, PC가 실행된 이후 메모리에 상주하는 핵심 프로그램인 커널과 사용자 사이를 연결해 주는 프로그램이다. 사용자가 직접 커널에 명령을 입력하는 것이다.

 

 Ubuntu에서 기본적으로 사용하는 것은 bash이다. 현재 나의 OS에서 사용하는 쉘의 종류는 다음과 같이 echo 명령으로 Shell을 확인하면 된다.

 

1.2 .bashrc

 앞에서 말한 bash의 각종 설정을 저장하는 파일이 몇 개 있다. 그 중 로그인한 사용자 개별로 지정한 설정을 저장해 두는 것이 bashrc 파일이다.

 '.bashrc'는 HOME 폴더에 위치해 있고, 파일 이름앞에 점이 붙어 시작되므로 숨김 파일임을 알 수 있다.

 

code ~/.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
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# 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

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# 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|*-256color) 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

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# 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 ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi
source /opt/ros/humble/setup.bash
export ROS_DOMAIN_ID=30

 

1.3 .bashrc에 명령 입력해 두기

 많이 사용하는 것이 필요한 명령을 .bashrc에 등록해 둔다.

 

2. .bashrc에서 alias 설정

 

2.1 alias 설정

 humble 버전의 ROS 중에서 apt install 명령으로 설치된 ROS pkg와 환경은 모두 sourc 명령으로 읽을 수 있다. 현재 설정해둔 alias은 아래와 같이 터미널에 입력하여 확인할 수 있다.

 

2.2 source ~/.bashrc를 alias로 지정하기

자주 사용하는 명령어인 'source ~/.bashrc'를 alias로 지정하여 사용한다. 이제 sb라고만 입력하면, source ~/.bashrc를 수행하게 된다. 

 

3. ROS2 domain 설정

ROS2는 Data Distribution System을 이용한다. 데이터 분산 서비스(DDS)는 실시간 시스템의 real-time, scalable, dependable, high performance을 가능하게 하는 Object Management Group 표준 Publish/Subscribe 네트워크 커뮤니케이션 미들웨어이다. 

 

 같은 노드의 이름, 같은 토픽의 이름이 충돌날 수 있기 때문에 시스템 도메인을 별도로 지정해야 한다. 이렇게 도메인을 지정하면 같은 도메인의 ROS2 노드들은 서로를 발견하고 메시지를 주고받을 수 있다.

 

export ROS_DOMAIN_ID=<ID>

 

 .bashrc에 ROS_DOMAIN_ID를 설정할 수 있다.

 

728x90