Modifying bash prompt - bash

When I open my terminal in Mac OS X, the command prompt current reads:
James-MacBook:project1 sam$
project1 is the name of the current directory.
What I want is to display the full path instead of James-MacBook.
How do I achieve this?

Your current prompt appears to show the hostname and the basename of your current directory. That means that the bash prompt, PS1, is likely set to:
PS1='\h:\W\$ '
To get the full directory name, use \w in place of \W:
PS1='\h:\w\$ '
You can set this at the command prompt. To make it permanent, this command can go into ~/.bashrc or ~/.bash_profile or other depending on how your system is configured.
You can read more about the options for command prompts, for which there are many options, in the PROMPTING section of man bash. Regarding the \w and \W options mentioned above, man bash explains how they are used:
\wthe 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

Easy. This is not an OS X thing, but a bash thing. Try this:
export PS1='$(pwd): '
Then if you want to make it permanent, just edit your .bash_profile:
nano ~/.bash_profile
And place that first command in there.

Related

Open shell variable (assigned with path) using vim editor

I am assigning a shell variable say TEST1 in .bashrc like below:
TEST1='x/y'
In my Present directory (pwd), i have folders in following hierarchy:
pwd/x/y/ztext.c
I am at pwd. I want to open ztext.c file from bash shell by executing
vim $TEST1/ and Tab key
I expected shell will list ztext.c in screen. But it didnt. Since i know the file name, i entered like below:
vim $TEST1/ztext.c
vim was opening new file and not the existing file.
But cd $TEST1 is moving to x/y folder path. Problem is only when executed with vim like mentioned above
Please correct me what am i doing wrong here.
Note : I tried export and set keyword for the TEST1 variable. It didnt help. Observation is same.
I don't why it's not working - I just did it on my machine. Below I show
exactly what I'm seeing in my terminal:
mattb#flat:~/mytest
$ l -R
.:
x/
./x:
y/
./x/y:
ztest.c
mattb#flat:~/mytest
$ export TEST1='x/y'
mattb#flat:~/mytest
$ vim $TEST1/
Pressing Tab expands to:
vim x/y/ztest.c

Editing ~/.bash_profile doesn't customize command-line prompt properly

Related Issue: How to change the terminal prompt to just current directory?
I have added export PS1="\w\$ " to my ~/.bash_profile, but the prompt for the command line just displays this:
\w$
It recognizes the backslash escape for the $ character, but not for the filepath. It does the same thing when I use a capital 'W'.
The problem was that my terminal is running zsh. I created ~/.zshrc and wrote the following code:
PROMPT="%/\$ "
I referred to this resource to find the appropriate characters for displaying the current file path: ZSH Prompt Expansion.

bash: "which adb" returns nothing, but "command -v adb" returns what i'd expect

This is a bash weirdness that's been bugging me.
I'm on OSX.
I've installed the android SDK, and as a result the adb tool is located in a folder in my home directory. That folder appears in my path as reported by env as ~/Development/android_sdk_latest/platform-tools.
adb itself runs just fine, but when I do which adb the result is empty. If I do command -v adb, the result is the full path, as expected: /Users/me/Development/android_sdk_latest/platform-tools/adb
adb does not appear in my aliases.
What subtlety of bash paths or which am I in the dark on?
You've run into a subtle incompatibility between /bin/bash and the which command.
On my system (Linux Mint), the which command is actually a shell script, not a built-in command, and its first line is #! /bin/sh. That means that it uses /bin/sh's handling of the $PATH variable.
This can vary depending on how /bin/sh is set up (it's sometimes a symlink to /bin/bash), but a little experimentation shows that bash handles a literal ~ character in $PATH as if it were the full path to your home directory, but /bin/sh does not. Since you have
~/Development/android_sdk_latest/platform-tools
as one of the elements of your $PATH, bash (your interactive shell) can find the adb command, but sh (the shell used by which) cannot.
On some systems, apparently including your OSX system, which is a binary executable. Again, since it's not a bash script, it's not going to match bash's treatment of $PATH.
I recommend making two changes.
First, don't put a literal ~ in your $PATH. For example, to append the platform-tools directory to your $PATH, rather than this:
export PATH="$PATH:~/Development/android_sdk_latest/platform-tools" # BAD!
do this:
export PATH="$PATH:$HOME/Development/android_sdk_latest/platform-tools"
The $HOME will expand to the path to your home directory (when you run the export command, not when you use $PATH later). ~ is not expanded within double-quoted strings.
Second, rather than using the which command, use the type command that's built into bash. type will follow the rules of your current shell rather than those of /bin/sh, and it will be able to report shell functions and aliases that which is unable to see. It has several useful command-line options; type help type at a bash prompt for details.
The bash shell's treatement of ~ characters in $PATH is documented in the bash manual's section on Tilde Expansion.

How to change shell prompt in Unix?

I want to disable the Unix shell prompt character ($, #, %) which usually we see in terminal. Is there any command or setting which can do this? I am using Solaris OS.
By shell prompt character I mean:
>$
>#
You need to adjust your PS1 environment variable in your .profile file.
I guess you could set it to "" to have it empty.
ex:
export PS1=""
EDIT: it can also be in your .bashrc file, or any other shell you are using.
You can get fancy and put the host name in there. But basically you change the PS1 environment variable:
export PS1=hello
You can add this command in your ~/.bashrc file. Or other startup file, if you use another shell.
I suggest first check the man pages for the shell (whatever is yours? echo $SHELL) under shell variables.
There are four types of prompt strings(PS) PS1, PS2, PS3, PS4, for your problem PS1 adjustment is sufficient.
To check the current settings: echo $PS1
To change: PS1="" for the current session, to make it permanent export it in your ~/.bashrc or ~/.profile.
To make it permanent for the user: export PS1="whatever special characters you want"
for more special characters and examples you can visit here "http://linuxconfig.org/bash-prompt-basics"

How to suppress (or customize) Mac Terminal shell prompt

Currently in my Terminal, every shell prompt looks like ComputerName: FooDir UserName$. The UserName part simply wastes too much space out of my precious 80 columns. Is there a way to suppress it?
The prompt is defined by the environment variable PS1 which you can define in .bash_profile.
To edit it, open or create the (hidden) file .bash_profile:
nano .bash_profile
and add a line that says
export PS1=""
Between the quotation marks, you can insert what you would like as your terminal prompt. You can also use variables there:
\d – date
\t – time
\h – hostname
\# – command number
\u – username
\W – current directory (e.g.: Desktop)
\w – current directory path (e.g.: /Users/Admin/Desktop)
The default prompt for common Linux distributions would be \w $, which evaluates to ~ $ in your home directory or e.g. /Users $ somewhere else. There are also website (like this one) that can help you with building your prompt.
If you want to remove the UserName part, your choice would be \h: \w$.
Once you made your changes, save the file with Control+o, Return, Control+x.
Here's an excellent article with a full list of Variables and Colors:
Customize your Shell Command Prompt
For a simple, minimalistic prompt, you can try this. Add the following line to your .bash_profile or simply test it first by running it in your terminal:
export PS1="\[\033[0m\]\w\$ "
It'll look something like this:
Here's my Prompt (source), also very simple:
export PS1="\[\033[1;97m\]\u: \[\033[1;94m\]\w \[\033[1;97m\]\$\[\033[0m\] "
2019 onwards, MacOS default shell is Z Shell. To customize command prompt, add a file named .zshrc in user home and put following line that sets a PS1 environment variable with desired prompt format:
export PS1="[%n]%~> "
Open new terminal
This is result of following format expansion:
%n User name
%~ Current directory
See full list of available expansions here.
Your answer can be found right here:http://www.hypexr.org/bash_tutorial.php#vi at about the middle of the page. :)

Resources