프롬프트에 디렉토리를 표시하는 방법은 무엇입니까?

프롬프트에 디렉토리를 표시하는 방법은 무엇입니까?

프롬프트에서 $ 앞에 디렉토리를 표시하는 방법은 무엇입니까?여기에 이미지 설명을 입력하세요.

답변1

Bash 매뉴얼 페이지에는 쉘이 확장할 수 있도록 프롬프트에 넣을 수 있는 이스케이프 시퀀스 목록이 있습니다. "팁"을 보면 다음 표를 찾을 수 있습니다.

          \a     an ASCII bell character (07)
          \d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
          \D{format}
                 the  format is passed to strftime(3) and the result is inserted into the
                 prompt string; an empty format results in a locale-specific time  repre‐
                 sentation.  The braces are required
          \e     an ASCII escape character (033)
          \h     the hostname up to the first `.'
          \H     the hostname
          \j     the number of jobs currently managed by the shell
          \l     the basename of the shell's terminal device name
          \n     newline
          \r     carriage return
          \s     the  name  of  the  shell, the basename of $0 (the portion following the
                 final slash)
          \t     the current time in 24-hour HH:MM:SS format
          \T     the current time in 12-hour HH:MM:SS format
          \@     the current time in 12-hour am/pm format
          \A     the current time in 24-hour HH:MM format
          \u     the username of the current user
          \v     the version of bash (e.g., 2.00)
          \V     the release of bash, version + patch level (e.g., 2.00.0)
          \w     the current working directory, with $HOME abbreviated with a tilde (uses
                 the value of the PROMPT_DIRTRIM variable)
          \W     the  basename  of  the current working directory, with $HOME abbreviated
                 with a tilde
          \!     the history number of this command
          \#     the command number of this command
          \$     if the effective UID is 0, a #, otherwise a $
          \nnn   the character corresponding to the octal number nnn
          \\     a backslash
          \[     begin a sequence of non-printing characters,  which  could  be  used  to
                 embed a terminal control sequence into the prompt
          \]     end a sequence of non-printing characters

당신이 원하는 것은 \w이므로 이렇게 하세요

PS1="\w $ "

그러면 현재 쉘로 변경됩니다. 정의를 .profile에 넣어 변경하지 않고 유지할 수 있습니다.

답변2

이를 수행하는 모든 방법은 비표준이므로 주의하여 단일 쉘에만 설정하십시오.

일반적으로 원하는 동작을 달성하는 방법에는 두 가지가 있습니다.

이스케이프 시퀀스 사용

Nick 사용자가 bash에 대한 이 방법을 설명했습니다. 이는 bash 확장에 따라 다릅니다. 표준에서는 PS1매개변수 확장(예: 쉘 변수 확장)이 발생하고 쉘이 "!"를 과거 명령 번호만큼 확장해야 한다고 요구합니다(표준에서는 "\!"가 아니라 단일 느낌표를 설명합니다.

명령 대체 사용

이 방법은 ksh 사용자가 일반적으로 사용하지만 보안 위험이 있으므로 의도적으로 표준에 포함되지 않습니다. 표준에서는 셸이 가져온 PS1환경을 무시하도록 요구하지 않으므로 해당 환경에는 악의적인 행위자의 공격 명령이 포함될 수 있습니다.

표준의 향후 버전에서는 명령 대체를 표준화할 가능성이 높지만 PS1쉘이 가져온 PS1환경을 무시하도록 요구하거나 쉘의 명령 대체를 켜고 작동하도록 켠 후 명령을 설정해야 합니다.

관련 정보