Hiding the text before an input to only $-sign in Bash? - bash

I have this in .bashrc;
PS1='$'
However, I see this still in terminal:
mas-macbook:some/path mas$
I want
$

PS1 should already have been exported long before you get to your .bashrc file, at least for a login shell. In that case, setting PS1 should simply overwrite the value (not its export status).
One thing to keep in mind is that bash itself does not run your .bashrc file for a login shell. The actual sequence of execution is:
/etc/profile, if there.
first of ~/.bash_profile, ~/.bash_login or ~/.profile.
I'm fairly certain that, if you want .bashrc to run for a login shell, it has to be sourced from one of those above.
For example, /etc/profile may call /etc/profile.local or all the scripts in the /etc/profile.d/ directory. Similarly, my .bash_profile calls the following, if they exist:
/etc/bash.bashrc
~/.bashrc
with the following snippet:
if [ -e /etc/bash.bashrc ] ; then
source /etc/bash.bashrc
fi
if [ -e "${HOME}/.bashrc" ] ; then
source "${HOME}/.bashrc"
fi
When I change PS1 and echo "hello" in my .bashrc, but comment out the sourcing of it in .bash_profile, the prompt doesn't get changed (nor the string printed) when I log in. When I uncomment the sourcing, I get both the string printed and the prompt changed when I log in.
To make sure that your .bashrc is called for your login shells, put that echo hello statement just after setting PS1, then log in to check.
If it is being called when you log in, you can execute "export -p" from your shell to get a list of all the exported variables - make sure PS1 has a "declare -x" in front of it. If not, just change your .bashrc to export it as well:
export PS1='$'
If it's already exported, then something is changing it after your set statement. In that case, you'll need to actually look at the login execution path to see what's getting called before it gives you control.

I think you need the export keyword:
export PS1='$'

You may have to escape the $ symbol. This works in my system...
PS1="\$"
If that don't work, please run the command 'echo $PS1' and let us know the results. You may also have to send the .bashrc file - it possible that you have set the PS1 variable before including the global /etc/bashrc file. If that's the case, the global file will overwrite the variable. You will have to set the PS1 variable after including the /etc/bashrc file.

Related

The /etc/environment file is not executing certain commands

Given below are the contents of my /etc/environment file
alias ...="cd ../../"
alias ls="ls -al"
export blah="blah blah"
When I start new terminal session and change to sudo user as sudo su, only the export command has run, which I am able to verify using env. The aliases are not set.
If I run source /etc/environment the aliases get set as expected. Am I missing something? I also read that /etc/environment is only read when the system boots. Is that true?
I am running on RHEL 7.
The /etc/environment is intended for setting environment variables for every user on login. Therefore you don't need to use export in this file.
Adding alias into this file won't work, because this file is not a shell script and only accepts variable=value pairs.
/etc/environment is used by the PAM-env module and is agnostic to
login/non-login, interactive/non-interactive and also Bash/non-Bash,
so scripting or glob expansion cannot be used. The file only accepts
variable=value pairs.
It's not possible to export aliases or set them globally - they need to be set again in every shell instance.
The file you want to use is ~/.bashrc in a home directory of a user. This file gets executed every time a user opens a bash shell. So aliases and variables set in this file will have effect only on that shell.
You can also use /etc/bash.bashrc which is System-wide .bashrc file for interactive bash shells.
The reason why the export in your /etc/environment worked and actually created and env variable is that the pam-env parser specifically ignores export keyword to avoid confusion for people who don't know that /etc/environment is not a shell script.
You can see that in pam_env.c source code
/* skip over "export " if present so we can be compat with
bash type declarations */
if (strncmp(key, "export ", (size_t) 7) == 0)
key += 7;
Its available for example here - Linux-PAM/pam_env.c v0.79. See line 00234.

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"

Bash .profile not loading

I'm not sure what's happened but my ~/.profile is no longer loading.
Can anyone see something wrong with the following?
export PS1="\u#local [\w]# "
export EDITOR="subl -w"
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
alias vst="ssh -i ~/.ssh/vst root#vst"
I know for a fact using that PS1 like I am attempting to do it should be doing Peter#local [~/path/to/file]# but it's not.
Any ideas?
Does ~/.bash_profile or ~/.bash_login exist? If so, that'll be used instead of ~/.profile.
In Unix FAQ (for OS X) we can read:
Bash Startup Files
When a "login shell" starts up, it reads the file
/etc/profile and then ~/.bash_profile or ~/.bash_login or
~/.profile (whichever one exists - it only reads ONE of these,
checking for them in the order mentioned).
When a "non-login shell" starts up, it reads the file /etc/bashrc and then the file ~/.bashrc.
Note that when bash is invoked with the name sh, it tries to mimic the startup sequence of the Bourne shell (sh). In particular, a non-login shell invoked as sh does not read any dot files by default. See the bash man page for details.
So if you already have ~/.bash_profile, the file ~/.profile won't be automatically read by bash, therefore you can add the following lines in your ~/.bash_profile to load it:
# Load user profile file
if [ -f ~/.profile ]; then
. ~/.profile
fi

Cygwin shell doesn't execute .bashrc

After start the cygwin shell, it just locate in a wrong home dir:
xfire#codingme.com ~
$ pwd
/cygdrive/c/Users/xfire
But it used to be /home/xfire
xfire#codingme.com /etc
$ cat passwd | grep xfire
xfire:unused:22773:10513:U-CORP\xfire,S-1-5-21-527237240-725345543-682003330-12773:/home/xfire:/bin/bash
And the .bashrc in the /home/xfire was not executed, even I copy it to the /cygdrive/c/Users/xfire, it also doesn't work!
On cygwin, I add this to my ~/.bash_profile:
. ~/.bashrc
Some program add an "HOME" environment in windows registry and set the value to "C:\Users\xfire", that's why cygwin take that directory as the home. cygwin.com/faq-nochunks.html
On my version of cygwin I found that only ~/.profile was being execueted so added
if [ -e "${HOME}/.bash_profile" ]; then
source "${HOME}/.bash_profile"
fi
to the .profile file. My .bash_profile file contains another test for .bashrc and executes that from inside there. I also added the following two lines to my .bashrc file.
export BASH_ENV="${HOME}/.profile"
export ENV="${HOME}/.profile"
The first of these ensures that .profile gets executed in non-interactive terminals and the second ensures it gets executed in POSIX terminals. I found a very useful explanation of what get run and when in the Bash Reference Manual.
In your case it wouldn't help as you have an issue with the value of your HOME environment variable but this page comes up quite high on the list when searching for this issue.
You can also also set BASH_ENV variable, e.g., BASH_ENV='C:\DOCUME~1\dwyttenb\.bashrc'
~/.bash_profile is executed before the initial command prompt is returned to the user. After that, every time a new shell is opened, ~/.bashrc is executed.
Adding . ~/.bashrc at end of ~/.bash_profile has resolved the problem.
Now you can check existing aliases using alias command.
Find more details on github.

ubuntu bash scripting: configure file missing?

in "Bash Guide for Beginners", it's said:
Bash is the GNU shell, compatible with the Bourne shell and incorporating many useful features from other shells. When the shell is started, it reads its configuration files. The most important are:
/etc/profile
~/.bash_profile
~/.bashrc
however, in my ubuntu 11.10,
- there's no "~/.bash_profile": file explorer does not show it, and "ls -l ~/.bash_profile" says "No Such file or directory"
- there are "/etc/profile" and "~/.bashrc", but they don't show up in file explorer, only "ls -l /etc/profile" and "ls -l /.bashrc" shows the result.
is there something missing during my installation?
No, it's fine if those files aren't there, they'll just be ignored. To get a complete list of what's loaded and in what order, run man bash and check the section on INVOCATION (use "/" and type in INVOCATION to search)
Edit: saving #athos a man bash call ;)
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file
/etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes com‐
mands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This
may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and
~/.bashrc.
Here I discuss, how to set JAVA_HOME variable and the PATH variable to your Java installation.
First using the terminal open the .bashrc which is at your home.
gedit ~/.bashrc
Now add the following to the end of the file.
JAVA_HOME=/usr/lib/jvm/java
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
NOTE: If /usr/lib/jvm/java does not match the actual JAVA_HOME path in your environment, then set the actual JAVA_HOME, where you have installed Java in your machine.
Now run,
source ~/.bashrc
Then, try running the following commands and check whether your getting the appropriate responses:
echo $JAVA_HOME
/usr/lib/jvm/java
echo $PATH
:/usr/lib/jvm/java/bin
If it not work try after restarting
It also reads /etc/bashrc, which is probably present on your system.
I'm pretty sure that you also have ~/.profile (that one it reads as well) or ~/.bashrc.
If those files are missing, feel free to create them and fill with whatever you need.

Resources