Setting environment path variable in bashrc not testing at command line - bash

I'm trying to understand setting variables in .bashrc and .bash-profile.
If I add something to my .bashrc, like this
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export MMMM_HOME=/appl #added
#. ~/inits.bsh #added
. /appl/etc/mmmm.env #added
How can I check that it's set properly? I tried opening a new putty session, I tried typing bash, I tried at the command line [-z "$MMMM_HOME"] && echo "empty" || echo "Not empty" #-z: command not found...
I tried which MMMM_HOME and which $MMMM_HOME at the command line.
I googled it. I'm not sure about this...does anyone have any info? Thanks!

Two spaces are missing in your test. Here's the command with the right bash syntax:
[ -z "$MMMM_HOME" ] && echo "empty" || echo "Not empty"

Related

Wrong contents appearing in .bashrc and .bash_profile files in the ~/ directory instead of the expected user environment variables and values

I am currently in the ~/ directory in terminal on my Mac, and trying to configure the .bash_profile.
I am trying to issue the cat command to view its contents. The result is the following:
# The next line updates PATH for the Google Cloud SDK. if [ -f '/Users/iwill/Downloads/google-cloud-sdk/path.bash.inc' ]; then . '/Users/iwill/Downloads/google-cloud-sdk/path.bash.inc'; fi
# The next line enables shell command completion for gcloud. if [ -f '/Users/iwill/Downloads/google-cloud-sdk/completion.bash.inc' ]; then . '/Users/iwill/Downloads/google-cloud-sdk/completion.bash.inc'; fi export PATH="/usr/local/opt/ncurses/bin:$PATH"
I receive the following when I try to view the .bashrc file:
if [ -f '/Users/iwill/Downloads/google-cloud-sdk/path.bash.inc' ]; then . '/Users/iwill/Downloads/google-cloud-sdk/path.bash.inc'; fi
# The next line enables shell command completion for gcloud.
if [ -f '/Users/iwill/Downloads/google-cloud-sdk/completion.bash.inc' ]; then . '/Users/iwill/Downloads/google-cloud-sdk/completion.bash.inc'; fi
Any ideas why am I not seeing the user environment variables as I should be ?

'source' statement and 'function' declaration at end of .bashrc interfere with one another

At first I created a script to 'find' a file and switch to that directory. Alas, upon returning from the script, the 'cd' was unchanged. Directory changes within a script are local to that script. I forgot. Sue me.
So... I created that same code as a function in the middle of .bashrc. When I re-enter the Bash shell, the function is not defined or visible. So... I placed the function at the end of .bashrc and -- voila! -- it worked. Here is the function:
function goto {
if [[ "$1" == "" ]]
then
echo "[ERROR] $0 requires a filename as input."
echo "[INFO] Usage: $0 <filename> finds file and changes to that directory."
else
echo "[INFO] Looking for file: $1"
declare -x -a full_filepath=$(find . -name "$1")
if [[ "${full_filepath[0]}" == "" ]]
then
echo "[ERROR] Unable to find requested file $1. Exiting..."
else
local filepath=${full_filepath[0]%/*}
local filename=${full_filepath[0]##*/}
echo "[INFO] Switching to $filepath to locate $filename..."
cd $filepath
fi
fi
}
Now here's the problem. I had to move it after SDKMan's init code in .bashrc (ignoring the warning that #THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!). Not surprisingly, 'sdk' no longer works.
Is there a "right way" to include a function in .bashrc so that other scripts like SDKMan's can remain at the end, for whatever-in-gods-name reason it must be there...???
I uninstalled then reinstalled SDKMan and the functions are now working as is SDKMan.
The conditional they add is odd. It reminds me of the shortcuts in Perl.
Here is the code added to .bashrc:
#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="/home/peter/.sdkman"
[[ -s "/home/peter/.sdkman/bin/sdkman-init.sh ]] && source "/home/peter/.sdkman/bin/sdkman-init.sh
This works just as well:
if [[ -s "/home/peter/.sdkman/bin/sdkman-init-sh" ]]; then source "/home/peter/.sdkman/bin/sdkman-init-sh"; fi
but it's a few characters longer, I guess. And if they had used the var they just defined above it, it would be even shorter:
if [[ -s "$SDKMAN_DIR/bin/sdkman-init-sh" ]]; then source "$SDKMAN_DIR/bin/sdkman-init-sh"; fi
Barmar: You were right. Location in .bashrc doesn't matter. Thanks.
Wiimm: Thanks for the tip.
Mark: For good measure, I've exported the functions. Thanks.

How can I change the color of my bash PS1 based on the current working directory? [duplicate]

I have to work within three main directories under the root filesystem - home/username, project, and scratch. I want my shell prompt to display which of these top level directories i am in.
Here is what I am trying to do:
top_level_dir ()
{
if [[ "${PWD}" == *home* ]]
then
echo "home";
elif [[ "${PWD}" == *scratch* ]]
then
echo "scratch";
elif [[ "${PWD}" == *project* ]]
then
echo "project";
fi
}
Then, I export PS1 as:
export PS1='$(top_level_dir) : '
Unfortunately this is not working as I want. I get home : for my prompt when I am in my home directory, but if I switch to scratch or projects then the prompt does not change. I do not understand bash scripting very well so I would appreciate any help to correct my code.
You can hook into cd to change the prompt every time you are changing the working directory. I've asked myself often how to hook into cd but I think that I now found a solution. What about adding this to your ~/.bashrc?:
#
# Wrapper function that is called if cd is invoked
# by the current shell
#
function cd {
# call builtin cd. change to the new directory
builtin cd $#
# call a hook function that can use the new working directory
# to decide what to do
color_prompt
}
#
# Changes the color of the prompt depending
# on the current working directory
#
function color_prompt {
pwd=$(pwd)
if [[ "$pwd/" =~ ^/home/ ]] ; then
PS1='\[\033[01;32m\]\u#\h:\w\[\033[00m\]\$ '
elif [[ "$pwd/" =~ ^/etc/ ]] ; then
PS1='\[\033[01;34m\]\u#\h:\w\[\033[00m\]\$ '
elif [[ "$pwd/" =~ ^/tmp/ ]] ; then
PS1='\[\033[01;33m\]\u#\h:\w\[\033[00m\]\$ '
else
PS1='\u#\h:\w\\$ '
fi
export PS1
}
# checking directory and setting prompt on shell startup
color_prompt
Please try this method instead and tell us how it works e.g. how your prompt changes in your home directory, your project or scratch directory, and other directories besides those. Tell us what error messages you see as well. The problem lies within it.
Tell me also how you run it, if it's by script, by direct execution, or through a startup script like ~/.bashrc.
top_level_dir ()
{
__DIR=$PWD
case "$__DIR" in
*home*)
echo home
;;
*scratch*)
echo scratch
;;
*project*)
echo project
;;
*)
echo "$__DIR"
;;
esac
}
export PS1='$(top_level_dir) : '
export -f top_level_dir
If it doesn't work, try changing __DIR=$PWD to __DIR=$(pwd) and tell us if it helps too. I also would like to confirm if you're really running bash. Note that there are many variants of sh like bash, zsh, ksh, and dash and the one installed and used by default depends on every system. To confirm that you're using Bash, do echo "$BASH_VERSION" and see if it shows a message.
You should also make sure that you're running export PS1='$(top_level_dir) : ' with single quotes and not with double quotes: export PS1="$(top_level_dir) : ".

How to get shell to self-detect using zsh or bash

I've a question on how to tell which shell the user is using. Suppose a script that if the user is using zsh, then put PATH to his .zshrc and if using bash should put in .bashrc. And set rvmrc accordingly.
#!/usr/bin/env bash
export PATH='/usr/local/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
I've tried the following but it does not work : (
if [[ $0 == "bash" ]]; then
export PATH='/usr/local/bin:$PATH' >> ~/.bashrc
elif [[ $0 == "zsh" ]]; then
export PATH='/usr/local/bin:$PATH' >> ~/.zshrc
fi
# ... more commands ...
if [[ $0 == "bash" ]]; then
[[ -s '/Users/`whoami`/.rvm/scripts/rvm' ]] && source '/Users/`whoami`/.rvm/scripts/rvm' >> ~/.bashrc
source ~/.bashrc
elif [[ $0 == "zsh" ]]; then
[[ -s '/Users/`whoami`/.rvm/scripts/rvm' ]] && source '/Users/`whoami`/.rvm/scripts/rvm' >> ~/.zshrc
source ~/.zshrc
fi
If the shell is Zsh, the variable $ZSH_VERSION is defined. Likewise for Bash and $BASH_VERSION.
if [ -n "$ZSH_VERSION" ]; then
# assume Zsh
elif [ -n "$BASH_VERSION" ]; then
# assume Bash
else
# assume something else
fi
However, these variables only tell you which shell is being used to run the above code. So you would have to source this fragment in the user's shell.
As an alternative, you could use the $SHELL environment variable (which should contain absolute path to the user's preferred shell) and guess the shell from the value of that variable:
case $SHELL in
*/zsh)
# assume Zsh
;;
*/bash)
# assume Bash
;;
*)
# assume something else
esac
Of course the above will fail when /bin/sh is a symlink to /bin/bash.
If you want to rely on $SHELL, it is safer to actually execute some code:
if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
# assume Zsh
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then
# assume Bash
else
# assume something else
fi
This last suggestion can be run from a script regardless of which shell is used to run the script.
Just do echo $0
it says -zsh if it's zsh and -bash if it's bash
EDIT: Sometimes it returns -zsh and sometimes zsh and the same with bash, idk why.
A word of warning: the question you seem to have asked, the question you meant to ask, and the question you should have asked are three different things.
“Which shell the user is using” is ambiguous. Your attempt looks like you're trying to determine which shell is executing your script. That's always going to be whatever you put in the #! line of the script, unless you meant your users to edit that script, so this isn't useful to you.
What you meant to ask, I think, is what the user's favorite shell is. This can't be determined fully reliably, but you can cover most cases. Check the SHELL environment variable. If it contains fish, zsh, bash, ksh or tcsh, the user's favorite shell is probably that shell. However, this is the wrong question for your problem.
Files like .bashrc, .zshrc, .cshrc and so on are shell initialization files. They are not the right place to define environment variables. An environment variable defined there would only be available in a terminal where the user launched that shell and not in programs started from a GUI. The definition would also override any customization the user may have done in a subsession.
The right place to define an environment variable is in a session startup file. This is mostly unrelated to the user's choice of shell. Unfortunately, there's no single place to define environment variables. On a lot of systems, ~/.profile will work, but this is not universal. See https://unix.stackexchange.com/questions/4621/correctly-setting-environment and the other posts I link to there for a longer discussion.
You can simply try
echo $SHELL
the other answers fail with set -u
if [ ! -z ${ZSH_VERSION+x} ]; then
echo "this is zsh"
echo ${(%):-%x}
elif [ ! -z ${BASH_VERSION+x} ]; then
echo "this is bash"
echo $BASH_SOURCE
else
echo "not recognized"
fi
An alternative, might not work for all shells.
for x in $(ps -p $$)
do
ans=$x
done
echo $ans
Myself having a similar problem, settled for:
_shell="$(ps -p $$ --no-headers -o comm=)"
if [[ $_shell == "zsh" ]]; then
read -q -s "?Do it?: "
fi
elif [[ $_shell == "bash" || $_shell == "sh" ]]; then
read -n 1 -s -p "Do it [y/n] "
fi
Here is how I am doing it based on a previous answer from Gilles :
if [ -n "$ZSH_VERSION" ]; then
SHELL_PROFILE="$HOME/.zprofile"
else
SHELL_PROFILE="$HOME/.bash_profile"
fi
echo "export VAR1=whatever" >> $SHELL_PROFILE
echo "INFO: Refreshing your shell profile: $SHELL_PROFILE"
if [ -n "$ZSH_VERSION" ]; then
exec zsh --login
else
source $SHELL_PROFILE
fi

Bash script: only echo line to ~/.bash_profile once if the line doesn't yet exist

I wrote a bash git-install script. Toward the end, I do:
echo "Edit ~/.bash_profile to load ~/.git-completioin.bash on Terminal launch"
echo "source ~/.git-completion.bash" >> ~/.bash_profile
The problem is, if you run the script more than once, you end up appending this line multiple times to ~/.bash_profile. How do I use bash scripting with grep or sed (or another option you may recommend) to only add the line if it doesn't yet exist in the file. Also, I want to add the line to ~/.profile if that file exists and ~/.bash_profile doesn't exist, otherwise just add it to ~/.bash_profile.
Something like this should do it:
LINE_TO_ADD=". ~/.git-completion.bash"
check_if_line_exists()
{
# grep wont care if one or both files dont exist.
grep -qsFx "$LINE_TO_ADD" ~/.profile ~/.bash_profile
}
add_line_to_profile()
{
profile=~/.profile
[ -w "$profile" ] || profile=~/.bash_profile
printf "%s\n" "$LINE_TO_ADD" >> "$profile"
}
check_if_line_exists || add_line_to_profile
A couple of notes:
I've used the . command instead of source as source is a bashism, but .profile may be used by non-bash shells. The command source ... is an error in .profile
I've used printf instead of echo because it's more portable and wont screw up backslash-escaped characters as bash's echo would.
Try to be a little more robust to non-obvious failures. In this case make sure .profile exists and is writable before trying to write to it.
I use grep -Fx to search for the string. -F means fixed strings, so no special characters in the search string needs to be escaped, and -x means match the whole line only. The -qs is common grep syntax for just checking the existence of a string and not to show it.
This is proof of concept. I didn't actually run this. My bad, but it's Sunday morning and I want to go out and play.
if [[ ! -s "$HOME/.bash_profile" && -s "$HOME/.profile" ]] ; then
profile_file="$HOME/.profile"
else
profile_file="$HOME/.bash_profile"
fi
if ! grep -q 'git-completion.bash' "${profile_file}" ; then
echo "Editing ${profile_file} to load ~/.git-completioin.bash on Terminal launch"
echo "source \"$HOME/.git-completion.bash\"" >> "${profile_file}"
fi
How about:
grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile || echo "source ~/.git-completion.bash" >> ~/.bash_profile
or in a more explicit (and readable) form:
if ! grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile; then
echo "Updating" ~/.bash_profile
echo "source ~/.git-completion.bash" >> ~/.bash_profile
fi
EDIT:
You should probably add an additional newline before your one-liner, just in case ~/.bash_profile does not end in one:
if ! grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile; then
echo "Updating" ~/.bash_profile
echo >> ~/.bash_profile
echo "source ~/.git-completion.bash" >> ~/.bash_profile
fi
EDIT 2:
This is a bit easier to modify and slightly more portable:
LINE='source ~/.git-completion.bash'
if ! grep -Fx "$LINE" ~/.bash_profile >/dev/null 2>/dev/null; then
echo "Updating" ~/.bash_profile
echo >> ~/.bash_profile
echo "$LINE" >> ~/.bash_profile
fi
The -F and -x options are specified by POSIX and were suggested in several other answers and comments.
# Decide which profile to add to
PROFILE=~/.bash_profile
if ! [ -e "$PROFILE" ] && [ -e ~/.profile ]; then
PROFILE=~/.profile
fi
# Add to profile if it doesn't appear to be there already. Err on the side of
# not adding it, in case user has made edits to their profile.
if ! grep -s 'git-completion\.bash' "$PROFILE"; then
echo "Editing $PROFILE to load ~/.git-completion.bash on Terminal launch"
echo "source ~/.git-completion.bash" >> "$PROFILE"
fi

Resources