How do I update my zsh prompt when an env variable changes? - environment-variables

I'm using oh-my-zsh. My theme file looks like this:
PROMPT="${AWS_PROFILE}%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
It's based on a popular theme. The only thing I have added is ${AWS_PROFILE}.
The prompt does show my AWS profile as intended. But even if I the environment variable changes the prompt doesn't change. This is not as I had intended. It does change if I run source ~/.zshrc.
Can I make the prompt update when my env variable changes?

Copying #chepner's comment into an answer:
AWS_PROFILE is expanded immediately when you define PROMPT, since you used double quotes. Use single quotes, or consider using a precmd hook to set the value of PROMPT.

Related

Rename environment variable key

I have following environment variable OLD_KEY=value and want to rename the env var name so it is KEY=value.
Already tryied export KEY=${${OLD_KEY}}.
Anyone who has an idea?
Is a POSIX shell, like bash, you dereference a variable with a single dollar-sign. So this is all you need:
export KEY="$OLD_KEY"
The double-quotes are always a good idea, even if you don't think they're needed due to quirks of the POSIX shell standard. Specifically, how variable expansion interacts with $IFS.
P.S., You should have told us which shell (or language) you're using rather than make us guess.

CLI - Ignore a line when using the for command

I'm trying to set a variable which will identify the make of a laptop.
I am doing this by using the command
wmic csproduct get vendor
This gives the following output
Vendor
LENOVO
(blank)
So based on that command, I have used the for command in the way below, to try and set a variable with the value 'LENOVO'
for /f "skip=1 tokens=1 delims=" %i in ('wmic csproduct get vendor')
do set vendor=%i
however, problem is that the output of the wmic command actually produces a blank line, under the word LENOVO, so my variable gets set as a blank value. Is there anyway to stop the for command from parsing this 3rd line, therefore stopping once the variable has been set with the value of 'lenovo'?
The skip function works fine, and bypasses the first line completely. However it doesn't seem to give me the option to say for example, skip lines 1 and 3 but leave 2. I have experimented with the EOL parameter to try and ignore the blank line, but the for command still reads the empty 3rd line each time.
Many Thanks
A little whacky, but try using the following within your line. It will filter out the Vendor line and then sort alphabetically, putting the blank line first. If you really need to do stuff like this regularly, check into a Windows port of some Unix utils like sed and awk.
wmic csproduct get vendor | find /v /i "vendor" | sort

Temporary Environment Variables in Ruby

Try to bear with me as I am fairly new to this and don't have much coding experience.
Im trying to use a ruby script to add a location to my PATH variable. So far I just have
path = ENV['PATH'].to_s
ENV['PATH'] = path + ";" + location
print ENV['PATH']
The problem is that the file seems to be added to the PATH and prints with it, but when I go check my path variable the new location is not there.
Also, when I run a separate script of which is one line:
print ENV['PATH']
the new location is not there either.
My question is is there a way to make the new PATH "save" instead of reverting to the old PATH when the script is finished?
If i am not mistaken you cannot really edit the environment variables.
When loading your script ruby loads all the currently known environment variables and adds the values to ENV.
When editing it, it will only be changed temporarily for the current execution.
If you want to change it and want it to persist you will have to e.g. use system
system("export PATH=$PATH:YOUR_PATH")
Same as you would do it in the CLI
The best you can do is generate a shell command to be evaluated outside the Ruby script and inside the shell you are running.
Something like this should work:
puts "export PATH=#{ENV['PATH']};#{location}"
Then, in the shell you do
eval $(ruby_script)
However, since you seem to want to run this in Windows you probably want to use command substitution, in which case you output the location directly:
puts location
And in the Windows shell:
set PATH=%PATH%;(ruby_script)
colon ":" is the field separator for PATH not semicolon ";" in Unix. Your example worked just fine for me today when I changed the semicolon to a colon.

How to add a set path only for that batch file executing?

Basically, I know I can go through my control panel and modify the path variable. But, I'm wondering if there is a way to through batch programming have a temporary path included? That way it is only used during that batch file execution. I don't want to have people go in and modify their path variables just to use my batch file.
Just like any other environment variable, with SET:
SET PATH=%PATH%;c:\whatever\else
If you want to have a little safety check built in first, check to see if the new path exists first:
IF EXIST c:\whatever\else SET PATH=%PATH%;c:\whatever\else
If you want that to be local to that batch file, use setlocal:
setlocal
set PATH=...
set OTHERTHING=...
#REM Rest of your script
Read the docs carefully for setlocal/endlocal , and have a look at the other references on that site - Functions is pretty interesting too and the syntax is tricky.
The Syntax page should get you started with the basics.
There is an important detail:
set PATH="C:\linutils;C:\wingit\bin;%PATH%"
does not work, while
set PATH=C:\linutils;C:\wingit\bin;%PATH%
works. The difference is the quotes!
UPD also see the comment by venimus
That's right, but it doesn't change it permanently, but just for current command prompt.
If you wanna to change it permanently you have to use for example this:
setx ENV_VAR_NAME "DESIRED_PATH" /m
This will change it permanently and yes, you can overwrite it in another batch script.

ZSH `rvm-prompt` on RHS prompt not refreshing

I have rvm-prompt feeding into my RPrompt, however it is not refreshing between commands: (Larger image)
For example, when I cd from one ruby project to another with an .rvmrc file pointing to a new gemset, the rprompt simply will not refresh. It appears that it must be caching the rprompt for performance purposes, so I am curious as to how I can force a refresh for zsh at each command?
How do you generate the prompt? I do it like this:
local rvm_ruby=' %{$fg[red]%}[$(~/.rvm/bin/rvm-prompt i v g s)]%{$reset_color%}'
And then use the rvm_ruby variable in my prompt:
PROMPT="${user_host}${directory}${git_branch}${rvm_ruby}%B
→%b "
EDIT: Note that the place where you create the contents for the variable needs single quotes, otherwise the command will get substituted right away and not updated anymore. This initially took me a bit to figure out. You may have the same problem defining your RHS prompt.

Resources