![Bashrc는 SSH 명령에 별칭을 추가하여 정의된 경로에 직접 연결하고 cd합니다.](https://linux55.com/image/75176/Bashrc%EB%8A%94%20SSH%20%EB%AA%85%EB%A0%B9%EC%97%90%20%EB%B3%84%EC%B9%AD%EC%9D%84%20%EC%B6%94%EA%B0%80%ED%95%98%EC%97%AC%20%EC%A0%95%EC%9D%98%EB%90%9C%20%EA%B2%BD%EB%A1%9C%EC%97%90%20%EC%A7%81%EC%A0%91%20%EC%97%B0%EA%B2%B0%ED%95%98%EA%B3%A0%20cd%ED%95%A9%EB%8B%88%EB%8B%A4..png)
개발 서버에 연결하는 별칭이 있습니다.
alias sshDev='ssh -p 22 username@ip_address'
연결하고 자동으로 새 디렉터리로 전환할 수 있기를 원합니다.
cd ../my-favorite-directory
하나의 로컬 별칭으로 두 명령을 모두 실행하는 방법이 있습니까?
답변1
다음과 같이 명령의 별칭을 설정할 수 있습니다.
alias sshDev="ssh -tp 22 username@ip_address 'cd /path/to/dir; bash'"
Arthur2e5가 제안한 대로 매개변수를 추가하는 것이 -il
편리합니다 .
관련 부품 man bash
:
-i If the -i option is present, the shell is interactive.
-l Make bash act as if it had been invoked as a login shell (see INVOCATION below).
동적 대상 폴더를 갖고 싶다면(별칭이 호출될 때마다 정의) 별칭에 의해 호출되는 함수를 작성해야 합니다. 예를 들어:
alias sshDev=ssh·Dev
function ssh·Dev() {
if [ "$#" -eq 0 ]; then
fav_dir="/path/to/dir"
else
fav_dir=$1
fi
ssh -tp 22 username@ip_address "cd $fav_dir; bash"
}
이 작은 코드 조각을 사용하여 다음과 같이 명령줄에 별칭을 호출(입력)할 때( sshDev
인수 없이) 함수에 정의된 정적 /path/to/dir을 사용합니다. 그렇지 않으면 다음과 같이 별칭 호출 sshDev /one/other/path/to/dir
인라인으로 제공된 경로를 사용합니다.