Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


The bash prompt

The prompt is the text you see when you have a Terminal window open. It's called a 'prompt' because it's prompting you to enter a command.

In your home directory (~), there should be a file named .bash_profile or .bashrc

This file is created by the shell, but you can alter it.

If you are running Catalina or newer, the file you need to edit is
.bash_profile in your home directory
See Stack Overflow for more
2024-12-03 11:39:24
[~/httpd/bash] trudge: cd

2024-12-03 11:50:13
[~] trudge:

2024-12-01 11:54:26
[~] trudge: ls -al
total 656
drwxr-xr-x+  61 trudge  staff    1952 Dec  1 11:02 .
drwxr-xr-x    9 root    admin     288 Nov  4 13:08 ..
-rwxr-xr-x    1 trudge  staff       8 Mar  8  2022 .CFUserTextEncoding
drwx------@   2 trudge  staff      64 Jan  2  2023 .DDLocalBackups
drwx------@   2 trudge  staff      64 Nov 26  2023 .DDPreview
-rw-r--r--@   1 trudge  staff   38916 Dec  1 11:29 .DS_Store
drwx------+  12 trudge  staff     384 Nov 29 18:56 .Trash
drwxr-xr-x@   5 trudge  staff     160 Jan  2  2023 .android
-rw-------    1 trudge  staff   11485 Dec  1 11:00 .bash_history
-rwxr-xr-x@   1 trudge  staff     956 Dec  1 11:08 .bash_profile
drwxr-xr-x   85 trudge  staff    2720 Dec  1 11:08 .bash_sessions
.
.
.
drwxr-xr-x  442 trudge  staff   14144 Nov 24 11:22 bin
drwxr-xr-x@  33 trudge  staff    1056 Nov 20 13:44 httpd
2024-12-01 11:54:32
[~] trudge:

My prompt displays 2 lines of text containing useful (to me anyway) information.

If either file does exist, open it in a text editor (BBEdit or TextEdit).

If for some reason it does not exist, open a NEW file in your text editor, and save it as:
.bash_profile in your home directory.

That is a dot (period) at the beginning and needs to be there.

In an existing file, look for a line starting with 'PS1='. That is the line defining what your Prompt String is.

At the beginning of that line, add an octothorpe (hash mark): #. That tells the shell that this line is a comment, and will not get executed.

This also saves the original PS1 definition in case you want to go back to it. Just delete the '#' and any other lines beginning with PS1.

If you are creating a NEW file, add the following line (copy & paste) at the top of the file:

export PS1='\D{%Y-%m-%d %H:%M:%S}\n[\w] \u: '

That is how I create my prompt, and it looks very confusing right now, but here is what it means:

• export
  This makes a variable available to other shell programs you run
• PS1
  This is the variable that holds your Prompt String
• \D
  This starts a Date definition held in curly braces { }
• {%Y-%m-%d %H:%M:%S}
  The format of the date & time
• %Y renders 4 digits for year
• - a hyphen
• %m 2 digit month with leading 0
• - a hyphen
• %d 2 digit day of month with leading 0
•   a space
• %H 2 digit hour (24 hour clock) with leading 0
• : a colon
• %M 2 digit minute with leading 0
• : a colon
• %S 2 digit second with leading 0
• \n a newline character
• [\w] the current working directory in square brackets
•   a space
• \u the current user logged in
• : a colon
•   a space

Note the whole string is in single-quotes.

2024-12-03 11:50:13
[~] trudge:

The prompt is one of the things you can experiment with. Anything you do with it is cosmetic and can be reversed, so don't be shy.

More about the prompt here and here.

Coding