bash, "history" 명령에 매개변수 전달

bash, "history" 명령에 매개변수 전달

나는 기록을 보다 합리적으로 만들기 위해 다음을 수행합니다(즉, 문제 해결 시 명령이 실행된 시기를 확인하는 것이 매우 중요할 수 있습니다).

shopt -s histappend;   # Append commands to the bash history (~/.bash_history) instead of overwriting it   # https://www.digitalocean.com/community/tutorials/how-to-use-bash-history-commands-and-expansions-on-a-linux-vps
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"   # -a append immediately, then  -c clear history, then -r read history every time a prompt is shown instead of after closing the session.
export HISTTIMEFORMAT="%F %T  " HISTCONTROL=ignorespace:ignoreboth:erasedups HISTSIZE=1000000 HISTFILESIZE=1000000000   # make history very big and show date-time
alias h='history';   # Note: 'h 7' will show last 7 lines

괜찮습니다. 하지만 필요할 때 원시 기록 출력을 얻을 수 있었으면 좋겠습니다. ("역사적 원본") 에는 작동 ho하지만 "ho 7"은 더 이상 사용할 수 없습니다.

alias ho="history | awk '{\$2=\$3=\"\"; print \$0}'" # 'history original'

그래서 다음을 시도했지만 오류로 인해 실패했습니다.

function ho() { history $1 | awk '{\$2=\$3=\"\"; print \$0}'; } # 'history original'

ho 7이 작업을 수행 하고 마지막 7줄만 볼 수 있는 별칭이나 함수를 어떻게 만들 수 있습니까 ?

답변1

거의 다 왔습니다. 함수를 정의하고 있지만 alias키워드를 사용하고 있습니다. 그냥 삭제 alias하면 괜찮을 것입니다. 다음으로 awk 변수를 이스케이프하지만 큰따옴표를 사용하지 않으므로 이스케이프된 값이 에 전달됩니다 awk.

ho() { history "$@" | awk '{$2=$3=""; print}'; }

답변2

"historical raw"라는 말은 타임스탬프 없이 출력을 원한다는 뜻이라고 가정합니다. 그렇다면 HISTTIMEFORMAT공백으로 설정하세요 history.

HISTTIMEFORMAT= history

별칭에서는

alias ho='HISTTIMEFORMAT= history'

관련 정보