Bash prompt changed in terminal - bash

Suddendly my bash prompt changed to
[80-254-70-241]myusername # ~ $
when in my home directory.
I have the following command in my .bash_profile export PS1="\[\033[32m\][\h]\u # \W \$\[\033[0m\]
I think the expression in square brackets supposed to be the name of the current directory.
What does this number 80-254-70-241 mean and how do I change it back?

The string "80-254-70-241" comes from the parameter "\h", which denotes your hostname. I think you got an IP and a hostname from DHCP so the prompt changed accordingly.
If you think this is annoying, you can replace "\h" with your preferred hostname (hardcoded).

Related

Bash - Error parsing ~ in environment variable

I have environment variable export MY_WORK_DIR="~/project".
While I'm using below command, it give me an error:
realpath $MY_WORK_DIR
realpath: '~/project': No such file or directory
In my guess, the ~ is not processed while using this env variable.
BTW, export MY_WORK_DIR=~/project is not an option for me. ~ should be in the string.
Could you please guide me how to get real path from envrionment variable ~/project ?
EDIT
Sorry. The variable is from other app so I cannot modify the environment variable which contains tilde. (Storing variable with tilde expanded form is not an option).
EDIT2
Is it safe to use eval command like this? eval "echo ${MY_WORK_DIR}". It works for my use.
I wouldn't use eval if I can avoid it. Especially in the way you are doing it, this is an invitation to do havoc by embedding dangerous code into MY_WORK_DIR.
A cheap solution for your concrete example would be to do a
if [[ ${MY_WORK_DIR:0:1} == '~' ]]
then
MY_WORK_DIR="$HOME/${MY_WORK_DIR:1}"
fi
which chops off the annoying ~ and prepends your home directory. But this would fail if MY_WORK_DIR is set to, say, ~einstein/project.
In this case, you would have to extract the user name (einstein) and search the home directory for this user.
Following steps can provide a resolution:
You need to replace "~" with the full path of the project directory.
Use pwd command to identify the full path of the project directory; e.g. /root/Documents/project is the full path you get.
Execute this command export MY_WORK_PROJECT=/root/Documents/project
Execute this command echo $MY_WORK_PROJECT so you should get this result
/root/Documents/project

MAC OS prompt won't change to show my full path

I have the following in my .bash_profile:
PS1='\h:\w$ '
But my prompt looks like this:
laptop:~$
What setting do I need to modify so that the "~" instead prints out the correct path... in this instance it should be /Users/jay/
The jay account is the default user, so is there a way to change this?
This is expected. Tilde is a very well known shorthand for active user's home folder.
If you look at the help for Bash
man bash
and then type ( you may need to hit 'n' key a couple of times to get to the section about PROMPTING)
/PROMPTING
You will notice that it says
\w the current working directory, with $HOME abbreviated with a tilde
\W the basename of the current working directory, with $HOME abbreviated with a tilde
That being said, if you really want it to print the full path instead, you can use another variable: $PWD (Peek Working Directory) to replace your \w or \W
So, type:
echo $PS1
If for example that returns
\h:\W \u\$
Type
PS1='\h:$PWD \u\$ '
That should change it (it works on my 10.11)

Customizing Terminal on Mac

I was wondering if there was any way to adjust how the terminal on Mac looks. Mainly, is there any way I could change the '$' after my name to be something like '>' after my name. Thanks!
To get a non-permanent preview you can just do
export PS1=">"
To make this permanent add this command to your .bash_profile.
To add the date, directory or other information, you can add any of the following to the text inside the quotes after PS1= :
\d – Current date
\t – Current time
\h – Host name
# – Command number
\u – User name
\W – Current working directory
\w – Current working directory with full path

$MSYSTEM variable in bashrc

NOTE
I am using Windows 7. On installing msysgit and GitHub for Windows, I found that git bash can be called from the folders. I opened up the terminal and first thing I wanted was to change how it displays in the console.
Here is what echo $PS1 gave me:
\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u#\h \[\033[33m\w$(__git_ps1)\033[0m\] $
I've been modifying my bash's PS1 for some time now, and know most of the content there is. But I have never ever seen $MSYSTEM before.
Google resulted in nothing except some results about using it to set $MSYSTEM=MINGW32 which of course isn't the case here.
So, what does MSYSTEM variable do? Also, when I create a file .bashrc and put this line there; the terminal now shows a blank-space just before my username. This is because of the empty space in this segment \007 \033 but it was absent before. Here are the screenshots when I use a custom .bashrc and when I don't:
Without bashrc
With bashrc
I know why the newline is absent from my customized terminal; but the questions are:
What is $MSYSTEM?
Why is the first blank-space space missing in first case?
It sets the Window title to the value of MSYSTEM variable. As far as the
space, it looks like you messed up the final newline, notice carefully
export PS1='\[\033]0;$MSYSTEM:\w\007
\033[32m\]\u#\h \[\033[33m\w\033[0m\]
$ '
in the variable above each start of a new line insert a literal newline
character into the PS1.
How to change the title of an xterm

How to use Windows environment variables in Vim script?

I have a Vim script that calls an external shell script and reads the output into the current buffer. It works fine in Unix systems. I'm trying to make the script OS-agnostic, so I want it to work for Windows users too. Here's the relevant line from the script:
:exe ":0r !$HOME/bin/shell_script"
According to Vim's docs, $HOME should translate fine for Windows users too. Using gvim on Win XP in command mode, doing
:echo $HOME
does indeed produce "C:\Documents and Settings\my_user".
However, my Vim script (adjusted for backslashes) fails on the Windows machine with a message in the DOS cmd.exe window saying
$HOME\bin\shell_script" not found.
In other words, Vim appears not to be expanding the value of $HOME before passing it to cmd.exe.
I can't use %APPDATA% either because Vim interprets % as the current file and pre/appends the file name to APPDATA. Weird that Vim does expand % but doesn't expand $HOME.
How do I get $HOME expanded correctly? Is it because I'm using exe in the vim script?
You don't need ! to read a file.
:exe ":0r $HOME/bin/shell_script"
Or read type command in windows(like cat in linux):
:exe '0r !type "'. $HOME . '\bin\shell_script"'
Note:
the type is executed in windows shell, so you need \(backslash) in path
if $HOME contains spaces, you need "(double-quote) to preserves the literal value of spaces
To clarify the answer given by kev:
On windows the $HOME variable do not expand properly when you escape to the console. For example, consider this code:
:e $HOME/myscript
This works because vim expands $HOME as normal. On the other hand this won't work:
:%! $HOME/myscript
Why? Because vim passes everything after ! to the underlying shell, which on Windows is cmd.exe which does environment variables %LIKE_THIS%. If you try to use that notation, vim will jump in and expand % to the name of the current file - go figure.
How to get around it? Use exe keyword:
:exe "%! ".$HOME."\myscript"
Let's analyze:
:exe is a command that takes a string and evaluates it (think eval in most languages)
"!% " the escape to shell command. Note that it is quoted so that exe can evaluate it. Also note how there is an extra space there so that when we append the home var it does not but right against it
.$HOME the dot is a string concatenation symbol. The $HOME is outside of the quotes but concatenated so that vim can expand it properly
."/myscript" path to script and possible arguments (also quoted)
The important thing here is keeping $HOME outside of the quote marks, otherwise it may not be properly expanded on Windows.
You probably need something like the expand function. For example:
:echo expand("$HOME/hello")
/home/amir/hello
You can find out more info about expand() with :help expand.
Here you have some information about slashes and backslashes in vim:
http://vimdoc.sourceforge.net/htmldoc/os_dos.html
When you prefer to use forward slashes, set the 'shellslash' option.
Vim will then replace backslashes with forward slashes when expanding
file names. This is especially useful when using a Unix-like 'shell'.

Resources