Linux
bash shell Rules
Sh-YE
2024. 4. 25. 16:03
1. Metacharacters
: shell에서 특별히 의미를 정해 놓은 문자들
- \ ? () $ ... * % {} [] 등
ex) echo * : 현재 디렉토리 전체표시
echo ??? : 현재디렉토리에서 3글자인 목록 출력
[test@localhost ~]$ ls
date.txt message.txt 공개 문서 비디오 서식
hello222.txt test 다운로드 바탕화면 사진 음악
[test@localhost ~]$ echo *
date.txt hello222.txt message.txt test 공개 다운로드 문서 바탕화면 비디오 사진 서식 음악
[test@localhost ~]$ echo ????
test 다운로드 바탕화면
2. Quoiting Rule
: 메타문자의 의미를 제거하고 단순문자로 변경.
BackSlash(\) | \ 바로 뒤의 메타문자는 특별한 의미를 제거 |
Double Quotes("") | " " 내의 모든 메타문자의 의미를 제거. 단,$, ' 은 제외 |
Single Quotes('') | ' ' 내의 모든 메타문자의 의미를 제거 |
ex1) 이름이 *** 인 파일 생성
touch \*\*\*
ex2) 이름이 ' ' 인 파일 생성
touch ' '
3. Nesting Commands
: command 안에 또 다른 command
ex1) echo "Today is $(date)" 는 $ 의미를 살려서 아래와 같이 출력
[test@localhost ~]$ date
2024. 05. 14. (화) 11:57:46 KST
[test@localhost ~]$ echo "today is date"
today is date
[test@localhost ~]$ echo "today is $(date)"
today is 2024. 05. 14. (화) 11:58:50 KST
ex2) ' ' 는 모든 문자를 문자취급하므로 아래와 같이 출력된다.
[test@localhost ~]$ echo 'today is $(date)'
today is $(date)
활용예시)
[test@localhost ~]$ touch report_$(date +%y%m%d)
[test@localhost ~]$ ls
report_240514
4. alias
1) alias 등록 : alias alias이름=실행할 명령
ex1) c라는 명령어를 입력하면 clear가 실행됨
alias c=clear
ex2) rm이라는 명령어를 입력하면 rm-i가 실행됨 ( *rm-i : 지우기 전에 한번 더 물어봐주는 명령어)
alias rm='rm -i'
2) alias확인
alias
3) alias 삭제 : unalias alias이름
unalias c
5. prompt
1) 프롬포트 설정 확인 : echo $PS1
[test@localhost ~]$ echo $PS1
[\u@\h \W]\$
- Bash shell에서만 prompt모양에 적용가능한 특수문자가 존재.
\h 호스트이름
\u 사용자이름
\w 작업 디렉토리 - 절대경로
\W 작업 디렉토리 - 상대경로
2) 프롬포트 변경 : PS1='변경할 프롬포트'
[test@localhost ~]$ PS1='[PROMPT TEST] '
[PROMPT TEST]
* alias, prompt 변경 한내용은 종료되면 사라진다.
영구적으로 사용하려면 홈디렉토리의 vi .bashrc 에 등록해줘야 한다.
[PROMPT TEST] ls -a
. .bash_history .bash_profile .cache .mozilla
.. .bash_logout .bashrc .config report_240514
[PROMPT TEST] vi .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
alias c=clear
~
~
~
~
~
~
~
~