사용자 도구

사이트 도구


linux:bash

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
linux:bash [2021/02/11 15:33]
kwon37xi [set -x]
linux:bash [2024/02/07 08:41] (현재)
kwon37xi [File Path dir/filename 분리]
줄 1: 줄 1:
 ====== Bash ====== ====== Bash ======
-===== bash-it ===== +  * https://www.gnu.org/software/bash/manual/bash.html
-  [[https://github.com/Bash-it/bash-it|bash-it]] bash framework. 다양한 bash 확장 +
-  * [[https://github.com/ohmybash/oh-my-bash|oh-my-bash]] bash 확장. bash-it 이 더 인기 좋음성능저하가 있었음(2018)+
  
 ===== shell template ===== ===== shell template =====
줄 9: 줄 7:
 ===== Redirect ===== ===== Redirect =====
   * http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/   * http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/
 +  * [[https://catonmat.net/bash-one-liners-explained-part-three|Bash One-Liners Explained, Part III: All about redirections]]
   * ''command-name &>file'' : 명령의 표준 출력과 표준 에러를 모두 file로 지정   * ''command-name &>file'' : 명령의 표준 출력과 표준 에러를 모두 file로 지정
   * ''cat textfile.txt > somefile 2>&1'' : 표준 출력은 somefile로 지정되고 표준에러도 표준출력(핸재 somefile)로 함께 보낸다.   * ''cat textfile.txt > somefile 2>&1'' : 표준 출력은 somefile로 지정되고 표준에러도 표준출력(핸재 somefile)로 함께 보낸다.
줄 18: 줄 17:
   * ''$@''   * ''$@''
   * ''$?'' : 직전 명령 exit code. ''0''이면 정상 종료.   * ''$?'' : 직전 명령 exit code. ''0''이면 정상 종료.
 +  * ''$-'' : 현재 shell 의 설정
 +
 +===== $- =====
 +  * 현재 shell 의 설정값
 +  * [[https://stackoverflow.com/questions/42757236/what-does-mean-in-bash|shell - What does $- mean in Bash? - Stack Overflow]]
 +  * ''H'' : histexpand
 +  * ''m'' : monitor
 +  * ''h'' : hashall
 +  * ''B'' : braceexpand
 +  * ''i'' : interactive
  
 ==== 직전 명령의 argument 대체 ==== ==== 직전 명령의 argument 대체 ====
줄 30: 줄 39:
   * ''!*'' : 모든 인자들   * ''!*'' : 모든 인자들
   * ''$$'' : 현재 스크립트 혹은 shell 의 PID   * ''$$'' : 현재 스크립트 혹은 shell 의 PID
 +  * ''$!'' : 직전 명령의 PID [[https://www.cyberciti.biz/faq/how-to-return-pid-of-a-last-command-in-linux-unix/|How to return pid of a last command in Linux / Unix - nixCraft]]
 ===== 실행 결과 변수에 저장 ===== ===== 실행 결과 변수에 저장 =====
 <code sh> <code sh>
줄 38: 줄 48:
 test=`basename "$file"` test=`basename "$file"`
 </code> </code>
 +
 +===== 환경변수에 저장된 환경변수 이름으로 값 얻기 =====
 +  * [[https://unix.stackexchange.com/questions/229849/indirectly-expand-variables-in-shell|bash - Indirectly expand variables in shell - Unix & Linux Stack Exchange]]
 +  * ''!환경변수이름을가진변수'' : 붙여써야 한다.
 +<code sh>
 +foo=bar
 +test=foo
 +echo ${!test}
 +</code>
 +
 +
 +===== 계산 하기 =====
 +  * ''%%$((계산식))%%'' 로 계산결과를 받을 수 있다.
 +  * [[https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html#Shell-Arithmetic|Shell Arithmetic (Bash Reference Manual)]]
 +
 +<code sh>
 +$ echo "2 + 3 = $((2+3))"
 +2 + 3 = 5
 +</code>
 +
 ===== 16진수/10진수(hex/dec)간 변환 ===== ===== 16진수/10진수(hex/dec)간 변환 =====
 <code sh> <code sh>
줄 56: 줄 86:
 [[http://stackoverflow.com/questions/8789729/how-to-zero-pad-a-sequence-of-integers-in-bash-so-that-all-have-the-same-width|numbers - How to zero pad a sequence of integers in bash so that all have the same width?]] 포맷팅된 숫자로 for loop 돌기 [[http://stackoverflow.com/questions/8789729/how-to-zero-pad-a-sequence-of-integers-in-bash-so-that-all-have-the-same-width|numbers - How to zero pad a sequence of integers in bash so that all have the same width?]] 포맷팅된 숫자로 for loop 돌기
 <code sh> <code sh>
 +# -f "%0숫자g" : 숫자만큼 0채우기
 +# -w : 제일큰 숫자를 기준으로 알아서 0 채워주기
 for i in $(seq -f "%05g" 10 15) for i in $(seq -f "%05g" 10 15)
 do do
줄 111: 줄 143:
 ===== VI 모드/ Emacs 모드 ===== ===== VI 모드/ Emacs 모드 =====
   * VI 에디터 편집 모드로 명령행을 변경한다.   * VI 에디터 편집 모드로 명령행을 변경한다.
-  * [[http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/|Working Productively in Bash's Vi Command Line Editing Mode (with Cheat Sheet)]] <code sh>+  * [[http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/|Working Productively in Bash's Vi Command Line Editing Mode (with Cheat Sheet)]] 
 +  * [[https://www.computerhope.com/unix/bash/bind.htm|Bash bind builtin command help and examples]] 
 +<code sh>
 set -o vi set -o vi
 </code> </code>
줄 138: 줄 172:
  
 ===== Shortcuts ===== ===== Shortcuts =====
 +  * [[https://blog.ssdnodes.com/blog/cheatsheet-bash-shortcuts/|Cheatsheet: Productivity-boosting Bash shortcuts | Serverwise]]
   * [[https://ostechnix.com/list-useful-bash-keyboard-shortcuts/|The List Of Useful Bash Keyboard Shortcuts - OSTechNix]]   * [[https://ostechnix.com/list-useful-bash-keyboard-shortcuts/|The List Of Useful Bash Keyboard Shortcuts - OSTechNix]]
 +  * [[https://www.youtube.com/watch?v=iKzoYUErEM0|Custom Bash config - Set up of .bashrc/.inputrc files for a fast and efficient shell experience - YouTube]]
 +  * ''.inpurc'' 등을 통해 bash 용 단축키를 만들 수 있다.
  
 ===== History에 시간 남기기 ===== ===== History에 시간 남기기 =====
줄 158: 줄 195:
 dirname "/path/to/filename.ext" # /path/to dirname "/path/to/filename.ext" # /path/to
 basename "/path/to/filename.ext" # filename.ext basename "/path/to/filename.ext" # filename.ext
-</code>+basename "/path/to/filename.ext" .ext # filename - 확장자까지 제거
  
 +
 +# filename 이라는 환경변수가 있을때
 +filename="filename.ext"
 +
 +# 확장자 (extension) 추출
 +"${filename##*.}" 
 +# 확장자를 제외한 파일 이름 추출
 +"${file%.*}"
 +</code>
 +  * [[linux:realpath|realpath]] : full 경로 확인
 ===== Filename name / extension 분리 ===== ===== Filename name / extension 분리 =====
   * [[http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash|파일 이름에서 이름과 확장자 분리하기]]<code sh>   * [[http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash|파일 이름에서 이름과 확장자 분리하기]]<code sh>
줄 190: 줄 237:
 fi fi
 </code> </code>
 +
 +===== environment variable empty 검사 혹은 기본값 지정 =====
 +  * 따옴표로 감싸야 globbing 과 문자열 분할을 막을 수 있다.
 +  * [[https://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash/28085062#28085062|Assigning default values to shell variables with a single command in bash - Stack Overflow]]
 +<code sh>
 +# if 문
 +if [ -z "${VARIABLE}" ]; then 
 +    FOO='default'
 +else 
 +    FOO=${VARIABLE}
 +fi
 +
 +# VARIABLE 환경변수가 존재하지 않으면 empty 이면 FOO를 default 로 설정
 +FOO="${VARIABLE:-default}
 +
 +# VARIABLE 환경변수가 존재하지 않거나 empty 이면 FOO와 VARIABLE을 모두 default 로 설정
 +FOO="${VARIABLE:=default}"
 +
 +# 첫번째 명령행 인자에 대해 존재하지 않으면 DEFAULTVALUE 환경변수의 값으로 지정하기
 +FOO="${1:-$DEFAULTVALUE}"
 +
 +# chainig도 된다.
 +DOCKER_LABEL=${GIT_TAG:-${GIT_COMMIT_AND_DATE:-latest}}
 +</code>
 +
  
 ===== test ===== ===== test =====
줄 241: 줄 313:
 ===== shopt ===== ===== shopt =====
   * [[https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html|The Shopt Builtin (Bash Reference Manual)]]   * [[https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html|The Shopt Builtin (Bash Reference Manual)]]
 +
 +==== shopt extglob ====
 +  * globbing 시에 추가할 파일이 아닌 제외할 파일을 지정할 수 있다.
 +  * [[https://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu|bash - How can I use inverse or negative wildcards when pattern matching in a unix/linux shell? - Stack Overflow]]
 +  * 제외할 glob 을 ''!(<glob>)'' 으로 지정한다.
 +<code sh>
 +shopt -s extglob
 +# *Music* 을 제외하고 복사
 +cp !(*Music*) /target_directory
 +
 +# extglob 끄기
 +shopt -u extglob
 +</code>
  
 ===== Special parameters ===== ===== Special parameters =====
   * [[https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html|Special Parameters (Bash Reference Manual)]]   * [[https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html|Special Parameters (Bash Reference Manual)]]
 +
 +===== function 목록 / 정의 =====
 +  * [[https://stackoverflow.com/questions/4471364/how-do-i-list-the-functions-defined-in-my-shell|bash - How do I list the functions defined in my shell? - Stack Overflow]]
 +
 +<code sh>
 +# 함수 목록 보기
 +declare -F
 +declare -f # 정의 포함 전체 목록
 +# 함수의 정의 보기
 +declare -f <function이름>
 +# or
 +type <function이름>
 +</code>
 +
 +===== dot(.) =====
 +  * 현재 디렉토리를 가리키는 ''./xx/yy''와 혼란을 줌.
 +  * [[https://www.shell-tips.com/bash/source-dot-command/|How and When to Use the Dot Command in Bash?]]
 +  * ''. 다른파일'' : 은 ''source'' 명령과 같다.
 +
 +===== bash-it =====
 +  * [[https://github.com/Bash-it/bash-it|bash-it]] bash framework. 다양한 bash 확장
 +  * [[https://github.com/ohmybash/oh-my-bash|oh-my-bash]] bash 확장. bash-it 이 더 인기 좋음. 성능저하가 있었음(2018)
 +
 +===== background job =====
 +  * ''명령어 &'' : background로 실행.
 +  * ''(명령어 &)'' : background로 실행하되 관련 실행 정보가 화면에 뿌려지는 것을 막음(예: Done, PID등의 정보)
 +
 ===== 참조 ===== ===== 참조 =====
   * http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html   * http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
줄 264: 줄 376:
   * [[https://github.com/dylanaraps/pure-bash-bible|dylanaraps/pure-bash-bible: 📖 A collection of pure bash alternatives to external processes.]]   * [[https://github.com/dylanaraps/pure-bash-bible|dylanaraps/pure-bash-bible: 📖 A collection of pure bash alternatives to external processes.]]
   * [[https://www.ubuntupit.com/how-to-use-the-linux-export-command-in-everyday-computing/|How to Use the Linux Export Command in Everyday Computing]]   * [[https://www.ubuntupit.com/how-to-use-the-linux-export-command-in-everyday-computing/|How to Use the Linux Export Command in Everyday Computing]]
 +  * [[https://www.shell-tips.com/bash/math-arithmetic-calculation/|Math Arithmetic: How To Do Calculation in Bash?]]
 +  * [[https://www.cyberciti.biz/tips/linux-unix-pause-command.html|Bash add pause in shell script with bash pause command - nixCraft]]
linux/bash.1613025227.txt.gz · 마지막으로 수정됨: 2021/02/11 15:33 저자 kwon37xi