사용자 도구

사이트 도구


linux:bash

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
linux:bash [2022/04/22 11:13]
kwon37xi
linux:bash [2024/02/07 08:41] (현재)
kwon37xi [File Path dir/filename 분리]
줄 17: 줄 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 대체 ====
줄 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>
 +
  
 ===== 계산 하기 ===== ===== 계산 하기 =====
줄 66: 줄 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
줄 121: 줄 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>
줄 171: 줄 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
 +basename "/path/to/filename.ext" .ext # filename - 확장자까지 제거
 +
 +
 +# filename 이라는 환경변수가 있을때
 +filename="filename.ext"
 +
 +# 확장자 (extension) 추출
 +"${filename##*.}" 
 +# 확장자를 제외한 파일 이름 추출
 +"${file%.*}"
 </code> </code>
   * [[linux:realpath|realpath]] : full 경로 확인   * [[linux:realpath|realpath]] : full 경로 확인
줄 203: 줄 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 =====
줄 292: 줄 351:
   * [[https://github.com/Bash-it/bash-it|bash-it]] bash framework. 다양한 bash 확장   * [[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)   * [[https://github.com/ohmybash/oh-my-bash|oh-my-bash]] bash 확장. bash-it 이 더 인기 좋음. 성능저하가 있었음(2018)
 +
 +===== background job =====
 +  * ''명령어 &'' : background로 실행.
 +  * ''(명령어 &)'' : background로 실행하되 관련 실행 정보가 화면에 뿌려지는 것을 막음(예: Done, PID등의 정보)
  
 ===== 참조 ===== ===== 참조 =====
linux/bash.1650593614.txt.gz · 마지막으로 수정됨: 2022/04/22 11:13 저자 kwon37xi