commands in .bash-profile do not work - macos

I added an alias in my .bash_profile file in my home directory on Mac Leopard. For example,
alias preview = "open -a preview"
alias lsall = "ls -l"
When I try to run these commands from the command line, I get the message that command not found
Any idea what I might be doing wrong? Thanks!

You just need to lose the spaces around =, i.e.
alias preview="open -a preview"
alias lsall="ls -l"
You also need to name the file .bash_profile if you want it to be executed automatically when you start a new shell.

Related

mac terminal shortcut not working

I intend to set up mac terminal shortcut per instructions but it does not work. This is what I did:
open terminal
nano .bash-profile
#add a few aliases
alias dtt = 'do this thing'
alias gc='git commit -m'
#save the bash profile
source ~/.bash_profile
Afterwards the new alias are present in the bash file. However no autocomplete is present in terminal, if I type dtt, it won't change to 'do this thing'. The shortcut doesn't seem to work.
Anything amiss? Thanks
Alias is used to do something (in your ex. 'do this thing'), typing the command you chose ('dtt'). The terminal won't autocomplete anything.
In fact alias instructs the shell to replace one string with another when executing commands.
You could use it to abbreviate commands. e.g. alias pg="ping". If you type pg google.com it will exec "ping google.com"

Sometimes I'd like to know what my shells aliases/functions are before I execute them. Can I log them without executing them?

I'm not sure if this is a stupid question but I'll often run into the sinario were I don't know what my commands are set to are without having to open my .zshrc (I'm using zsh obviously).
For instance lets use gs. I'm not sure which of the following gs will execute:
alias gs="git status"
or alias gs="gulp serve"
or alias gs="grunt serve"
Is there a command, or something like echo gs, "$(gs)" or echo "$(gs)" that will log the command (e.g. git status) rather executing what ever I have gs set to?
You can run the alias command to print out all of your shell aliases. If you want to know what a particular alias resolves to, just pass it to the alias command as an argument:
➜ alias gs
gs='git status'

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)!

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.

.bash_history does not update in Git for Windows (git bash)

I am using Git for Windows (ver. 1.7.8-preview20111206) and even though I have a .bash_history file in my HOME folder, it never automatically gets updated. When I start Git Bash, I can see in the history commands that I manually added to the .bash_history file, but it does not get updated automatically.
I used the shopt -s histappend command to make sure that the history gets saved every time I close the shell, but it does not work.
If I manually use the history -w command, then my file gets updated, but I would want to understand why the shopt command does not work as I understand it should.
Anyone can tell me why is this behavior happening?
I put this in my ~/.bash_profile
PROMPT_COMMAND='history -a'
Create the following files
~/.bash_profile
~/.bashrc
And put the following line in both of them
PROMPT_COMMAND='history -a'
To do this from the console (Git Bash) itself, use the following commands:
echo "PROMPT_COMMAND='history -a'" >> ~/.bash_profile
echo "PROMPT_COMMAND='history -a'" >> ~/.bashrc
What history -a means
From the history --help command
-a append history lines from this session to the history file
What is PROMPT_COMMAND?
Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.
Difference between .bash_profile and .bashrc
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.
But, if you’ve already logged into your machine and open a new terminal window (xterm) then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.
On OS X, Terminal by default runs a login shell every time, so this is a little different to most other systems, but you can configure that in the preferences.
References
https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc
As it was said here, to save git bash history on Windows you must not close the terminal with X button. Use exit command instead. History of commands will be saved then regardless of configuration mentioned in the accepted answer.
If you're using Git bash in VSCode please see C.M.'s comment above.
This worked for running git's bash in Visual Studio Code, but I had to put it ~/.bashrc not ~/.bash_profile. – C.M. Jul 29 at 14:43
This solved it for me.
There is a more complete answer on Unix Stackexchange, by Pablo R. and LinuxSecurityFreak. Add the following to your ~/.bashrc:
# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
Please be careful, though:
The problem with this PROMPT_COMMAND solution is that the numbers for each history item changes after each command. For example, if you type history(1) ls (2) rm, then you do !1 to repeat (1), the history number might change and might run the rm command. Chris Kimpton
If you use git bash for windows 8, just put this in your ~/.bash_logout file:
history > .bash_history
Obviously you need a ~/.bash_history file.
Regards.
For me what worked was going into C:\Users\MY_USER\ and deleting the .bash_profile file.
Ps: I am using windows 10
I am using Windows 10 for me it was a permission problem, my temporary solution was to add Everyone group and give it Full control on ~/.bash_history file.
By the way, for those using the Portable version of Git for Windows, there's no need to create .bash_profile or .bashrc. Simply add to C:<path to your Git Portable folder>\etc\bash.bashrc:
PROMPT_COMMAND='history -a'
Found an answer in another post : https://superuser.com/questions/555310/bash-save-history-without-exit
If you want to have an history updated between two terminals.
As a window user I created a file .bash_profile inside my user folder. And then I add the following content : PROMPT_COMMAND='history -a;history -c;history -r'
Just run this in your git bash
echo 'HISTFILE=$HOME/.bash_history' >> $HOME/.bashrc

Resources