Unknown bash scripts running when terminal opens? - macos

I am not exactly sure what's happening here - I open a terminal window on my mac and see the following:
Last login: Tue Jun 26 00:36:08 on ttys002
-bash: : command not found
-bash: : command not found
This seems to me like some file is being executed whenever I open a new terminal window, but I have no idea how I'd find this file. Is there some list of files that run when terminal opens that I could find easily? I'd love to know what is happening here (and how it came about in the first place)

grep Sorry $(grep -l Thank /etc/profile /etc/bash* ~/.bashrc ~/.bash_profile ~/.profile) /dev/null
And (when you are lucky) you will find the places where are these strange commands with Thank and Sorry.
It is possible although does these lines are produced during some command substitution.
In that case you will not find the strings. I would recommend then add set -x to ~/.bash_profile to find the string that produces these messages.

Check .bashrc, .profile and .bash_profile. Specifically, I have a feeling you have a String marked with inverted commas, which is then being tried to execute
From the bash manual:
When bash is invoked as an interactive login shell, or as a
non-inter‐
active shell with the --login option, it first reads and executes com‐
mands 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 commands from the first one that
exists and is readable.

Related

Issue with environment variable on Mac OS sierra

I am seeing a strange problem with the storing of an env in mac os.
I set custom env in ~/.bash_profile
export MYENV=user
Then ran the . ~/.bash_profile and then I printed the env using
printenv then I can see the MYENV=user in the list.
If I close the terminal and reopen and execute printenv then I could not see MYENV in the list still I can see the export MYENV=user in ~/.bash_profile. It seems strange to me.
I am using Mac os High Sierra 10.13.6.
Could some body please tell me what mistake I am doing?
Note that ~/.bash_profile is only run for login shells. From the man page:
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 exe-
cutes commands from the first one that exists and is readable. The --noprofile
option may be used when the shell is started to inhibit this behavior.
So if you terminal isn't launching the shell with -l, --login or with $0 having a leading hyphen it won't be a login shell and thus won't read ~/.bash_profile. You may need to reconfigure how your terminal launches the shell if you want the shell to read that config script.
On the other hand ~/.bashrc is always read by an interactive shell. So if you put the export in that script it should do what you expect. It certainly does for me. You replied to Amila that it didn't work for you. So I'd suggest a simple experiment. Open two terminal windows. In one edit ~/.bashrc and add these two lines:
echo running .bashrc
export WTF=abc
In the other window just run bash. It should echo that message and echo $WTF should print abc. Now open a new terminal window. If you don't see that message and the env var isn't present then something is inhibiting reading that config script. Possibly the shell is being run with the --norc flag.
~/.bash_profile is executed before the initial command prompt is returned to the user, which means after a new login. Try adding the environment variable to ~/.bashrc instead.

Alias can't save forever on Mac?

I want to create my own alias to make some command more simpler.I add
alias ll='ls -l' in ~/.bashrc,like this:
ANDROID_NAME=/Users/smy/Library/Android/sdk
PYTHONPATH=/Library/Python/2.7/site-packages:$PYTHONPATH
PATH=$ANDROID_NAME/platform-tools:$PYTHONPATH:$PATH
export ANDROID_HOME
export PYTHONPATH
export PATH
#alias
alias ll='ls -l'
when I first add this alias to this file,I execute source command,like this:
source ~/.bashrc
then in this command window,it can works,but when I create a new command window,it can't recognize the ll alias,that is when I execute ll,such error exists:
-bash: ll: command not found
when I type source ~/.bashrc,it will work.
So my question is:
why the alias can't be recognized whenever I type it,why I must execute source command to make it work when new command window opened,and how I can resolve this. I'm working on mac,Anyone can teach me about this,thanks!
You need to use ~/.bash_profile or ~/.profile for login shells instead of ~/.bashrc. From the documentation:
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 commands from the first one that exists and is readable.
and:
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
When opening a new terminal window/tab, the shell should be opened as a login shell.
Insert the alias you need into ~/.bash_profile and ~/.bash
Go to Terminal -> Preferences -> Generals -> Shell opens with -> Command (complete path) -> /bin/bash
Only one things worked for me, and I haven't seen it mentioned yet.
In addition to
making sure that my .bash_profile file included the relevant alias/aliases
ensuring in the Terminal > Preferences > General window that the Shell opens with option selected is /bin/bash
I needed to ensure that the bash profile is loaded using bash -l each time a new window is opened. Probably spent 30m hunting for this, so hope it helps folks who are updating to the latest versions of their MacOS (like yours truly today)!

Zsh show fail every time when I open my terminal

I'm using a Mac with OS X Yosemite and Zsh.
By accident,I delete the content of three files below:
.bashrc
.bash_profile
.profile
After that ,when I open my terminal.
The Zsh will show fail under the last login information,it confused me ,and I want to know why.
You might want to look at a duplicate question: Zshell starts up with exit status of 1 after uninstalling RVM
It has an answer that solved the issue for me:
I found a .zlogin file on my system that contained some rvm-related code. I've deleted the code, and the problem is solved!
Zsh (by default) doesn't read from .bashrc, .bash_profile, or .profile, so the contents of these files shouldn't matter. You also didn't mention which .bashrc, .bash_profile, and .profile were erased… These files exist in both your /Users/username directory and /etc. The files sourced by zsh at startup are listed in the OS X zsh man page (man zsh in a terminal) under "STARTUP/SHUTDOWN FILES". The only reason it would call one of the previously mentioned files is if they were explicitly sourced in one of the default files.
My suggestions:
Check the contents of /etc/zshenv (this is the only zsh-specific file in my etc directory). Mine has only the following:
# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
Can you log in at all using zsh? If not, can you log in using another shell? You can do this in the OS X Terminal.app by going to Preferences -> General and changing the option for "Shells open with:" from "Default login shell" to Command (fill in another shell, i.e., /bin/bash or /bin/sh). If you can log in with any shell, try the following solution from this question:
Looking for the error
All shell output goes to the terminal, so you could just redirect it
when starting it. As you are looking for error messages during
initialisation, I'd suggest the following procedure:
Disable the problematic configurations
Open a terminal
Check the value of SHLVL: echo $SHLVL
Re-enable the configurations
Start a new z-shell from within the running shell with zsh 2> zsh-error.log, this redirects stderr to the file 'zsh-error.log'.
Check the value of SHLVL again. If it is bigger then previous value then exit the current shell (exit). (Explanation below)
Have a look at 'zsh-error.log' in the current directory.
If 'zsh-error.log' does not show anything, you may want to run zsh -x
2> zsh-error.log in step 5 instead. This provides a complete debug
output of anything zsh does. This can get quite huge.
As the answer suggests, those logs can get enormous if you are sourcing man files at startup. Just a bare shell should result in a reasonably small log file.
Finally, you can retrieve a list of all the files sourced by zsh on startup by running zsh -o sourcetrace.
Hope this helps.

Why do I need to source bash_profile every time

I have installed Hadoop and every time I want to run it, first I have to do this:
source ~/.bash_profile
or it won't recognize the command hadoop
Why is that?
I am on OSX 10.8
Now that we've narrowed down the problem:
Run ps -p $$ at the command line to check if you are, in fact, using a bash shell.
Realize that you are in zsh, which means you should be editing your profile in .zshrc.
Copy the offending lines from .bash_profile to .zshrc, OR
Modify your .zshrc to directly source your .bash_profile.
UPDATE: Do what #TC1 mentions in the comments and keep the shell-specific code in each shell's own profile, and from those profiles, only source shell-agnostic code.
On Mac Catalina, I just had to open "preferences" on terminal and change the "shells open with" from "default" to "Command(complete path)", which the default path was "/bin/zsh". touch ~/.zshrc, if that file doesn't exist already, and copy/paste your stuff from ".bash_profile" into the ".zshrc" file.
To elaborate, with terminal running, I opened "settings" from the Terminal menu on the Mac navbar. On the "General" tab, look for "Shells open with" select "Command (complete path)", and type in /bin/zsh.
bash_profile.sh is applicable for bash shell.
if your default shell is not bash and if your default shell is someother shell for example zsh then you have to manually load the .bash_profile using source ~/.bash_profile.
You can always change the default shell to bash shell so that the .bash_profile file will be automatically loaded.
Inorder to automatically load .bash_profile, you can update your default shell to bash using the command chsh -s /bin/bash
cat /etc/shells will list the default shells available in the
machine
echo $SHELL will display the currently active shell in your machine
To change active shell to a different shell, use chsh -s /bin/bash.
Then echo $SHELL to verify if the shell has changed.
Terminal -> Preference -> profile -> Shell -> Run command : source ~/.bash_profile
Tick on run inside shell.
After doing all those , just logout and check weather everything works fine or not
I tried the approved answer. Changing the .zshrc file works for one of my machines. But for the other one, when I run ps -p $$, it is -sh under the command. And I changed both bash and zsh files, neither of them works for me this time.
So I found this
https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
it mentioned
"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. "
so I went to that file /etc/profile and add "source ~/.bashrc" in that file. Then it works since every time a terminal is opened, it runs the command in that /etc/profile file.
Not sure if this is the best solution but it works.
sudo nano /etc/bashrc and change that, restarted the terminal and it finally remembered with command. Tried ~/.bash_profile and ~/.bashrc without success, just wasn't sourcing it.
Go to “Preferences/Profiles then look in the right window and find “shell”.
Once in that if your “Startup Run Command” hasn’t been turned on. Click the box to turn it on and in the command section type:
(If you made a .zsh file)
source .zsh ; clear
(If you made a .bash_profile)
source .bash_profile ; clear
Doing this ; clear
Will clear your terminal to a new page so that you don’t see your terminal display:
“Last login: etc
User#user-Mac ~ % source .zsh
If you typed the commands as I said you should just get this:
User#user-Mac ~ %
That way you will be greeted with a clear page with no extra jumbo. Also to make sure that your .zsh or .bash_profile aliases work type the following command to see a list of your custom aliases:
Alias
One alias I like to do is
alias LL=“ls -la”
This will display a tree or the directory you are in as well as hidden files.

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