Show working directory in bash

I have started to become familiar with the Terminal app and the book I'm using for this process (Mac OS X: The Missing Manual) shows the working directory and the short user name. When I start Terminal bash 3.2 does not show both of these by default. I would like to learn how to set bash to show both.

Thank you,

Bob

20" iMac 2.4GHz Intel - 667 MHz, Mac OS X (10.6.7), 3GB memory

Posted on Sep 13, 2011 5:01 PM

Reply
9 replies

Sep 13, 2011 8:04 PM in response to baxterpgh

pwd


will "Print Working Directory"


With respect to Linc's suggstion to add a PS1 setting to your shell initialization file, that would be one of the following in your home directory:


.bash_profile

.bash_login

.profile


1 and only 1 of the above files will be read by bash. It will read the first one its finds in the order listed above.


ls -a


will show files that start with a period, such as the shell initialization files.

Sep 24, 2011 2:02 PM in response to baxterpgh

There is also TextWrangler (free download), which is a very good text editor with a GUI interface.


TextEdit in Text mode is good, but it is not as easy to access hidden files via TextEdit, and a shell initialization file, because of the leading period (dot) is considered hidden.


Nano does not have this problem as you just type the name of the file on the command line, and TextWranger has an option for opening hidden files.

Sep 24, 2011 8:34 PM in response to baxterpgh

I didn't like the command line promt to take up half of the command line. I redefined the cd command to type out the current directory.


Place the following in the appropriate bash profle.



# Print current directory after change directory command cd

alias cd='cdir'



# Define a command to change a directory and list the resulting directory

function cdir ()


{


\cd "$*"

pwd


}

Sep 25, 2011 6:23 AM in response to rccharles

I have a similar aversion to very long command prompts, but I still want to have a hint as to what directory I am in, so I refine my prompt to be:


__cwd3() {
typeset start=${PWD/${HOME}/\~}
typeset delete=${start%/*/*/*}
typeset partial=${start#${delete}/}
if [[ "${partial}" != /* && "${partial}" != \~* ]]
then
partial="/.../${partial}"
fi
echo "${partial}"
}
PS1="\![\h]\$(__cwd3)> "


Where both the __cwd3() function and the PS1 definitions are in my bash shell initialization file.


__cwd3() only displays the last 3 subdirectories of your current working directory. If the path is relative to your home directory, /Users/username is replaced with ~ before removing any excess leading directories. If any leading directories are deleted, then /.../ is prepended.


\$(__cwd3) is dynamically executed each time your PS1 prompt is displayed, so the path information is always up-to-date.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Show working directory in bash

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.