Alias for bash not working (using bash and edited bash_profile with restart) - bash

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
Tony$ echo $SHELL
/bin/bash
Tony$ cd Dev/operation-fix/
:operation-fix Tony$ gst
-bash: gst: command not found
ALIASES in bash_profile in ~:
alias gst="git status"
Gone through a few other stackoverflows but the issue persists? Not sure why I am using bash_profile for other aspects of development like hidden environment variables and such and works fine. Am I not using the alias correctly? Thanks

Related

why is executing a command in the sh terminal failing

I am using ubuntu.
When I do this from the terminal
echo $SHELL
I got /bin/bash
and then when I do
echo "`$SHELL -c 'echo $BASH_VERSION'`"
I got 4.4.20(1)-release
Then, I try to do the same thing inside a particular docker container I am running, but as you will see this has the sh terminal not the bash terminal
so from that container
echo $SHELL
I got /bin/sh
that I have read is a link to dash
then with
echo $BASH_VERSION
I got
4.3.48(1)-release
so I do
echo "`$SHELL -c 'echo $BASH_VERSION'`"
and I got nothing.
Can someone explain me what is happening?
How can you run a sh terminal in ubuntu
Any aditional resource to understand shells is welcome too
I specially don't understand why when doing the echo $BASH_VERSION I got something but then with the latter I got nothing if the shell is the same

Source a tcsh script from bash

I'm trying to execute a tcsh script in a bash environment. My regular environment is tcsh so the following command works for me:
source /usr/scripts/my_setup -t abs
But I need to execute this command from some third party software which runs the commands on bash shell. This script should set an environment variable $TMP.
I already tried all the suggested solutions from the previous threads and they all do not work.
What I tried:
tcsh -c "source /usr/scripts/my_setup -t abs ; exec bash"
tcsh -c "source /usr/scripts/my_setup -t abs ; bash"
The following steps:
echo "tcsh -c 'source /usr/scripts/my_setup -t abs'" > setup
chmod a+x setup
source setup
They all fail. And I understand why (everytime I use tcsh it opens a new shell). But I can't seem to understand how to source a tcsh script from bash.

Source bash profile when starting interactive Docker session

Running this opens bash interactively:
docker exec -it bash
However, it's not sourcing ~/.bash_profile, ie the bash_profile in the container. I've tried bash -i instead of bash at the end, but doesn't work. So how to make profile load when entering the container, as it would in a regular interative shell?
add this to the container user .bashrc:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
You need to rebuild the image and copy the .bash_profile file too.
-l works, as mentioned by #chepner in comments:
docker exec -it bash -l

bash: parse_git_branch: command not found

This should be very simple.
I recently noticed that when I type 'bash' into Terminal on Mac it shows this:
Jays-MacBook-Pro: ~ $ bash
bash: parse_git_branch: command not found
When before it didn't. Can someone explain why and how to resolve.
It is likely that you configured BASH to run parse_git_branch and print the result as part of PS1 (or alike). You can check this by: "echo $PS1" and "echo $PROMPT_COMMAND".
However, parse_git_branch is not a builtin function of bash. Below is how I configured my PS1. You may want to copy my git_branch_4_ps1 as your parse_git_branch
PS1='\n' # begin with a newline
PS1=$PS1'\[\e[38;5;101m\]\! \t ' # time and command history number
PS1=$PS1'\[\e[38;5;106m\]\u#\h ' # user#host
PS1=$PS1'\[\e[7;35m\]${MY_WARN}\[\e[0m\] ' # warning message if there is any
PS1=$PS1'\[\e[38;5;10m\]${MY_EXTRA} ' # extra info if there is any
PS1=$PS1'\[\e[0;36m\]$(git_branch_4_ps1) ' # git_branch_4_ps1 defined below
PS1=$PS1'\[\e[38;5;33m\]\w' # working directory
PS1=$PS1'\n\[\e[32m\]\$ ' # "$"/"#" sign on a new line
PS1=$PS1'\[\e[0m\]' # restore to default color
function git_branch_4_ps1 { # get git branch of pwd
local branch="$(git branch 2>/dev/null | grep "\*" | colrm 1 2)"
if [ -n "$branch" ]; then
echo "(git: $branch)"
fi
}
If your parse_git_branch is defined in ~/.bash_profile, it will not be loaded when you open a non-login shell (e.g. by running bash).
The differences between login and non-login shells are described here: Difference between Login Shell and Non-Login Shell? For our purposes, the main difference is that login shells (e.g. that when you first open Terminal) automatically source ~/.bash_profile upon startup, whereas non-login shells (e.g. that when you run bash from within Terminal) do not.
To fix this error, simply source your ~/.bash_profile after running bash:
user#host:~ $ bash
bash: parse_git_branch: command not found
user#host:~ $ source .bash_profile
Alternatively, place the function in ~/.bashrc instead, which will be automatically sourced by non-login shells (as covered in the earlier link).
Instead of having
parse_git_branch
call in PS1 definition alone you may use
parse_git_branch 2>/dev/null
to send stderr to /dev/null. This will silence the error you don't want to see.
have you export your $PS1 ?
You can check by run command:
printenv
else you should export it by run:
export -n PS1
after you will can run sudo or sudo su without problem
The key to this is to NOT export PS1. If it's exported, then any non-login shell also takes PS1. Since .bash_profile is automatically source'd by the login shell, the PS1 variable only affects the login shell.

Running system command under interactive bash shell

I am trying to run a command that has been aliased in my ~/.bashrc from Perl using the system command. It works well running the command only once, but when I run it twice the second invocation is run as a background job and then suspended (the same as pressing <CTRL-Z>) and I have to type fg to complete the command. For example
use strict;
use warnings;
system ('bash -ic "my_cmd"');
system ('bash -ic "my_cmd"');
The second call never completes. The output is [1]+ Stopped a.pl.
Note:
The same result is obtained when replacing my_cmd with any other command, for example ls.
It seems not to depend of the contents of my ~/.bashrc file. I tried to remove everything from it, and the problem still persisted.
I am using Ubuntu 14.04 and Perl version 5.18.2.
Update
For debugging I reduced my ~/.bashrc to
echo "Entering ~/.bashrc .."
alias my_cmd="ls"
alias
and my ~/.bash_profile to
if [ -f ~/.bashrc ]; then
echo "Entering ~/.bash_profile .."
. ~/.bashrc
fi
Now running:
system ('bash -lc "my_cmd"');
system ('bash -lc "my_cmd"');
gives
Entering ~/.bash_profile ..
Entering ~/.bashrc ..
alias my_cmd='ls'
bash: my_cmd: command not found
Entering ~/.bash_profile ..
Entering ~/.bashrc ..
alias my_cmd='ls'
bash: my_cmd: command not found
and running
system ('bash -ic "my_cmd"');
system ('bash -ic "my_cmd"');
gives
Entering ~/.bashrc ..
alias my_cmd='ls'
a.pl p.sh
[1]+ Stopped a.pl
Rather than using the -i switch for an interactive shell, I think you should use the -l (or --login) switch, which causes bash to act as if it had been invoked as a login shell.
Using the -l switch doesn't load ~/.bashrc by default. According to man bash, in a login shell, /etc/profile/ is loaded, followed by the first file found from ~/.bash_profile/, ~/.bash_login or ~/.profile/. On my system, I have the following in ~/.bash_profile, so ~/.bashrc is loaded:
# Source .bashrc
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Now that your ~/.bashrc is being loaded, you need to enable the expansion of aliases, which is off in a non-interactive shell. To do this, you can add the following line before setting your aliases:
shopt -s expand_aliases
A process randomly stopping - aside from ctrl-z is usually when it needs STDIN, but doesn't have it attached.
Try it with - for example passwd &. It'll background and go straight into 'stopped' state. This may well be what's happening with your bash command. -i means interactive shell, explicitly, and you're trying to do something noninteractive with it.
That's almost certainly not the best approach, you probably want to do something different. bash --login might be closer to what you're after.
Tom Fenech's answer worked for me in Ubuntu 16.04.1 LTS with a small addition. At the top of my ~/.bashrc file, I commented out the following section so that if the shell is not interactive (e.g., a login shell), ~/.bashrc is still read. On some other versions of Linux I don't see this section.
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac

Resources